﻿

//using OmronLib;
//using System;
////using System.Collections.Generic;
////using System.Collections.ObjectModel;
//using System.Data;
////using System.Linq;
//using System.Net;
////using System.Text;
////using System.Threading;
//using System.Timers;
//using System.Windows;
////using System.Windows.Controls;
////using System.Windows.Data;
////using System.Windows.Documents;
////using System.Windows.Input;
//using System.Windows.Media;
////using System.Windows.Media.Imaging;
////using System.Windows.Navigation;
////using System.Windows.Shapes;
//using System.Collections;
////using System.Runtime.InteropServices;
////using System.Threading.Tasks;
//using System.IO;
//using System.Threading;
////using System.Threading.Tasks;

//namespace OmronHelper
//{
//    /// MainWindow.xaml 的交互逻辑
//    public partial class MainWindow : Window
//    {
//        private readonly System.Timers.Timer timer = new System.Timers.Timer(1000);//灯定时刷新
//        private readonly OmronLib.Omron omron = new OmronLib.Omron();
//        //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
//        object obj = new { Name = "lily", Age = 12, port = 9600, ip = "192.168.250.1" };

//        //定时循环
//        System.Timers.Timer t = new System.Timers.Timer(20000);//实例化Timer类，设置间隔时间为10000毫秒；

//        //主入库函数
//        bool run = false;
//        string ip;
//        string port;
//        public MainWindow()
//        {
//            object ob = new { Port = 9600, ip = "192.168.250.1" };
//            InitializeComponent();
//            timer.Elapsed += Timer_Elapsed;
//            timer.Start();

//            port = PortBox.Text;
//            ip = IPBox.Text;
//            WriteLogs("log.txt", "RunLog", "主函数启动");

//            InitializeComponent();
//            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//设置该属性 为false

//        }


//        /// 用个timer来更新界面上的灯
//        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
//        {
//            this.Dispatcher.Invoke(new Action(() =>
//            {
//                StatusLight.Fill = omron.IsConnected ? new SolidColorBrush(Colors.LimeGreen) : new SolidColorBrush(Colors.Red);
//            }));



//        }


//        /// 按钮手动连接
//        public void ConnectBtn_Click(object sender, RoutedEventArgs e)
//        {
//            try
//            {
//                if (string.IsNullOrEmpty(IPBox.Text) || string.IsNullOrEmpty(PortBox.Text))
//                {
//                    MessageBox.Show("IP和Port不能为空！", "连接失败");
//                }
//                else
//                {
//                    omron.IPAddr = IPAddress.Parse(IPBox.Text);
//                    int port;
//                    if (int.TryParse(PortBox.Text, out port))
//                    {
//                        omron.Port = port;
//                    }

//                    string info;
//                    if (!omron.PlcConnect(out info))
//                        MessageBox.Show(info, "连接失败");
//                    else
//                    {
//                        ReadBtn.IsEnabled = true;
//                        ReadBitBtn.IsEnabled = true;
//                        WriteBtn.IsEnabled = true;
//                        ReadBtn_Copy.IsEnabled = true;
//                        ReadBtn_Copy0.IsEnabled = true;
//                        ReadBtn_Copy1.IsEnabled = true;
//                        ReadBtn_Copy2.IsEnabled = true;
//                        run = true;
//                        Console.WriteLine("链接Plc成功");
//                        DateTime dt1 = DateTime.Now;
//                        StatusLight1.Fill = new SolidColorBrush(Colors.LimeGreen);
//                        WriteLogs("log.txt", "RunLog", "手动链接PLC");

//                        //定时循环
//                        t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件；
//                        t.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
//                        t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
//                        t.Start();

//                        //开新线程循环
//                        Program obj = new Program();
//                        Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString());
//                        Thread thread = new Thread(new ThreadStart(obj.Method1));
//                        //thread.Start();
//                    }
//                }
//            }
//            catch (Exception ex)
//            {
//                WriteLogs("log.txt", "ConnectBtn_Click:", ex.ToString());
//            }
//        }


//        /// 断开连接
//        private void DisconnectBtn_Click(object sender, RoutedEventArgs e)
//        {
//            omron.DisConnect();
//            ReadBtn.IsEnabled = false;
//            ReadBitBtn.IsEnabled = false;
//            WriteBtn.IsEnabled = false;
//            ReadBtn_Copy.IsEnabled = false;
//            ReadBtn_Copy0.IsEnabled = false;
//            ReadBtn_Copy1.IsEnabled = false;
//            ReadBtn_Copy2.IsEnabled = false;
//            StatusLight1.Fill= new SolidColorBrush(Colors.Red);
//            run = false;
//            WriteLogs("log.txt", "RunLog", "断开PLC");
//            t.Enabled = false;//是否执行System.Timers.Timer.Elapsed事件；
//            t.Stop();
//            //MainWindow.Exit();
//            //MainWindow.close();
//            //MainWindow.Exit(exitCode);
//            //DbHelperSQL.ExecuteScalar("eixt");
//        }


//        /// 读位
//        private void ReadBitBtn_Click(object sender, RoutedEventArgs e)
//        {
//            // 从D8100.0读到D8100.15
//            string[] res;

//            omron.ReadDBits(14440, 0, 16, out res);

//            // 输出
//            ValueBox.AppendText("读位 D14440.0-D14440.15    ");


//            foreach (string item in res)
//            {
//                ValueBox.AppendText(item);
//                ValueBox.AppendText(" ");
//            }
//            ValueBox.AppendText("\n");
//        }

//        /// 写
//        private void WriteBtn_Click(object sender, RoutedEventArgs e)
//        {
//            //把D8100的16个位全写1
//            //omron.WriteDBits(8100, 0, 16, new bool[] { true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true });
//            //把D8100的第一个位写0
//            //omron.WriteDBits(8100, 0, 1, new bool[] {false });
//            //把D8100写0
//            //omron.WriteDWords(8100, 1, new int[] { 0 });
//            //存入到数据库
//            //string dayAddress = "date,H0809,H0910,H1011,H1112,H1213,H1314,H1415,H1516,H1617,H1718,H1819,H1920,allh";
//            //string nightaddress = "date,h2021,h2122,h2223,h2324,h2401,h0102,h0203,h0304,h0405,h0506,h0607,h0708,allh";
//            //string[] JsonData = { "2021-09-14", "5555", "3343", "1011", "233", "333", "1314", "1415", "1516", "1617", "1718", "1819", "1920", "666" };
//            //string strSql = "insert into dayyeild(" + dayAddress + ") values('" + JsonData[0] + "','" + JsonData[1] + "','" + JsonData[2] + "','" + JsonData[3] + "','" + JsonData[4] + "','" + JsonData[5] + "','" + JsonData[6] + "','" + JsonData[7] + "','" + JsonData[8] + "','" + JsonData[9] + "','" + JsonData[10] + "','" + JsonData[11] + "','" + JsonData[12] + "','" + JsonData[13] + "')";
//            //DbHelperSQL.ExecuteSql(strSql);
//        }

//        //写位
//        private void WriteBits()
//        {
//            //把D8100的16个位全写1
//            ////omron.writedbits(8100, 0, a16, new bool[] { true, true,true, true, true, true, true, true, true, true, true, true, true, true, true, true });
//            //把D8100的第一个位写0
//            //omron.WriteDBits(8100, 0, 1, new bool[] {false });
//            //把D8100写0
//            //omron.WriteDWords(8100, 1, new int[] { 0 });
//            //存入到数据库

//        }



//        //-------------------------------绿点数据采集程序块-------------------------------------------------------------------------------------



//        //读字-产量采集--1
//        string[] DBtable = { "dayyeild", "nightyeild" };
//        string dayAddress = "date,H0809,H0910,H1011,H1112,H1213,H1314,H1415,H1516,H1617,H1718,H1819,H1920,dclass,type";
//        string clockRunLog = "";
//        private void ReadBtn_Click(object sender, RoutedEventArgs e)
//        {
//            ArrayList AllYeild = new ArrayList();
//            try
//            {
//                string[] res;
//            string str1 = "14440";//Madress.Text;
//            string conutNB = "24";// conut.Text;
//            if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(conutNB))
//            {
//                MessageBox.Show("地址和不能数量为空！", "连接失败");
//            }
//            else
//            {
//                omron.ReadDWords(int.Parse(str1), int.Parse(conutNB), out res);
//                    DateTime dt1 = DateTime.Now;
//                    while ((DateTime.Now - dt1).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    // 输出d地址
//                    //ValueBox.AppendText("读字:" + int.Parse(str1) + "=> ");
//                    clockRunLog = "读字:" + int.Parse(str1) + "=> ";
//                int i = 0;
//                int n = 0;
//                int m = 0;
//                var nStr = "";
//                var mStr = "";
//                Console.WriteLine("产量res:" + res + "res.length:", res.Length);
//                if (res.Length < 1)
//                {
//                    Console.WriteLine("产量res数据不对:", res);
//                    //ValueBox.AppendText("地址和不能数量为空！" + "连接失败\n");
//                    //ReadBtn_Click(null, null);
//                    WriteLogs("log.txt", "RunLog", "重新调用ReadBtn_Click");
//                    return ;
//                }
//                else
//                {
//                    foreach (string item in res)
//                    {
//                        //ValueBox.AppendText(item);
//                        //ValueBox.AppendText(" ");//添加空格
//                        var hex2To10 = Convert.ToInt32(res[i], 16);
//                        if (n == 0)
//                        {
//                            nStr = item;
//                            n++;
//                        }
//                        else
//                        {
//                            mStr = nStr + item;
//                            n = 0;
//                            //Console.WriteLine("Mstr:" + mStr);
//                            var hexTo10 = Convert.ToInt32(mStr, 16);
//                            AllYeild.Add(hexTo10);
//                            //Console.WriteLine("hexTo10:" + hexTo10);
//                            m++;
//                        }
//                        //System.Diagnostics.Debug.WriteLine("item+res:" + item + "-" + res[i]);
//                        i++;
//                    }
//                    //ValueBox.AppendText("\n");
//                    System.Diagnostics.Debug.WriteLine("Allyeild产量:" + AllYeild[0]);
//                    Console.WriteLine("AllYeild.length:" + AllYeild.Count); //使用数量
//                     //Console.WriteLine("AllYeild.length:" + AllYeild.Capacity);//容器容量
//                    //报存数据
//                    string type = "产量(pcs)";
//                        DateTime dt11 = DateTime.Now;
//                        while ((DateTime.Now - dt11).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    WriteLogs("log.txt", "RunLog", "调用InsertDB");
//                    InsertDB(DBtable, dayAddress, AllYeild, type);
//                }

//            }


//            }
//            catch (Exception ex)
//            {
//                WriteLogs("log.txt", "产量异常:", ex.Message.ToString());
//            }
//        }


//        //数据库操作-产量采集操作---1
//        private void InsertDB(string[] DBtable, string dayAddress, ArrayList AllYeild, string type)
//        {
//            try
//            {
//                string ldclass;
//            DateTime dt = DateTime.Now;
//            string dt24 = dt.ToString("HH");
//            string DT24 = dt.ToString("yyyyMMdd");
//            //DateTime.Parse(DT24).ToString("yyyyMMdd");
//            System.Diagnostics.Debug.WriteLine("时间:" + dt24 + "/" + int.Parse(dt24) + "/" + DT24);
//            string today = dt.ToString("yyyyMMdd");
//            if (int.Parse(dt24) < 8 || int.Parse(dt24) >= 20) //判断为晚班
//            {
//                //晚班产量数据
//                System.Diagnostics.Debug.WriteLine("班别：晚班" + dt24);
//                ldclass = "晚班";
//                if (int.Parse(dt24) < 8 && int.Parse(dt24) >= 0) //晚班时间为0-8之间减1判断
//                {
//                    System.Diagnostics.Debug.WriteLine("日期DT24：" + DT24);
//                    DT24 = (int.Parse(DT24) - 1).ToString();
//                    System.Diagnostics.Debug.WriteLine("日期DT24-1：" + DT24);
//                }
//                else
//                {      //时间为20-24之间
//                    System.Diagnostics.Debug.WriteLine("dt24" + dt24 + "DT24:" + DT24);
//                }
//                //查询是否已经存在记录
//                string SqlStatus = (DbHelperSQL.GetNightMaxID("id", "dayyeild", DT24, type)).ToString();
//                DateTime dt1 = DateTime.Now;
//                while ((DateTime.Now - dt1).TotalMilliseconds < 500)
//                {
//                    continue;
//                };
//                Console.WriteLine("SqlStatus0:" + SqlStatus);
//                string strSql;
//                if (string.IsNullOrEmpty(SqlStatus))
//                {
//                    StatusLight1.Fill = omron.IsConnected ? new SolidColorBrush(Colors.LimeGreen) : new SolidColorBrush(Colors.Red);
//                }
//                if (int.Parse(SqlStatus) <= 1)
//                {
//                    //晚班产量数据新增1条
//                    Console.WriteLine("查询晚班数据条不存在" + SqlStatus);
//                    strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[20] + "','" + AllYeild[21] + "','" + AllYeild[22] + "','" + AllYeild[23] + "','" + AllYeild[0] + "','" + AllYeild[1] + "','" + AllYeild[2] + "','" + AllYeild[3] + "','" + AllYeild[4] + "','" + AllYeild[5] + "','" + AllYeild[6] + "','" + AllYeild[7] + "','" + ldclass + "','" + type + "')";   //+ "9999" +"','"                                                                                                                                                                                                                                                                                                                                                                                                                          
//                    //Console.WriteLine("strSql0:" + strSql);                
//                }
//                else
//                {
//                    //晚班产量数据更新1条
//                    Console.WriteLine("查询数据条已经存在" + SqlStatus);
//                    strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[20] + ",H0910=" + AllYeild[21] + ",H1011=" + AllYeild[22] + ",H1112=" + AllYeild[23] + ",H1213=" + AllYeild[0] + ",H1314=" + AllYeild[1] + ",H1415=" + AllYeild[2] + ",H1516=" + AllYeild[3] + ",H1617=" + AllYeild[4] + ",H1718=" + AllYeild[5] + ",H1819=" + AllYeild[6] + ",H1920=" + AllYeild[7] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus;
//                    //Console.WriteLine("strSql1:" + strSql);

//                }

//                //Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));
//                int sqlok = DbHelperSQL.ExecuteSql(strSql);//数据库保存
//                DateTime dt12 = DateTime.Now;
//                while ((DateTime.Now - dt12).TotalMilliseconds < 500)
//                {
//                    continue;
//                };
//                if (sqlok == 1 && run == true)
//                {
//                    Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + sqlok);

//                    WriteLogs("log.txt", "RunLog", "ReadWaitTime2");
//                    //ReadWaitTime2(null, null);

//                }
//                else
//                {
//                    MessageBox.Show("数据库操作错误：", "数据库查询失败！");

//                }
//            }
//            else  //白班
//            {
//                ldclass = "白班";
//                System.Diagnostics.Debug.WriteLine("班别：" + ldclass + "时间：" + dt24);
//                //查询白班是否已经存在当天的记录
//                string SqlStatus1 = (DbHelperSQL.GetDayMaxID("id", "dayyeild", today, type)).ToString();
//                DateTime dt13 = DateTime.Now;
//                while ((DateTime.Now - dt13).TotalMilliseconds < 500)
//                {
//                    continue;
//                };
//                Console.WriteLine("SqlStatus1:" + SqlStatus1);
//                string strSql;//白班数据库操作膜版
//                if (int.Parse(SqlStatus1) <= 1)
//                {
//                    //白班产量数据新增1条
//                    Console.WriteLine("查询数据条不存在" + SqlStatus1);
//                    strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[8] + "','" + AllYeild[9] + "','" + AllYeild[10] + "','" + AllYeild[11] + "','" + AllYeild[12] + "','" + AllYeild[13] + "','" + AllYeild[14] + "','" + AllYeild[5] + "','" + AllYeild[16] + "','" + AllYeild[17] + "','" + AllYeild[18] + "','" + AllYeild[19] + "','" + ldclass + "','" + type + "')";
//                    Console.WriteLine("strSql1:" + strSql);

//                }
//                else
//                {
//                    //白班产量数据更新1条
//                    Console.WriteLine("查询数据条已经存在" + SqlStatus1);
//                    strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[8] + ",H0910=" + AllYeild[9] + ",H1011=" + AllYeild[10] + ",H1112=" + AllYeild[11] + ",H1213=" + AllYeild[12] + ",H1314=" + AllYeild[13] + ",H1415=" + AllYeild[14] + ",H1516=" + AllYeild[5] + ",H1617=" + AllYeild[16] + ",H1718=" + AllYeild[17] + ",H1819=" + AllYeild[18] + ",H1920=" + AllYeild[19] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus1;
//                    Console.WriteLine("strSql1:" + strSql);
//                }
//                int sqlok = DbHelperSQL.ExecuteSql(strSql);
//                if (sqlok == 1 && run == true)
//                {
//                    //StatusLight1.Fill = omron.IsConnected ? new SolidColorBrush(Colors.LimeGreen) : new SolidColorBrush(Colors.Red);
//                    Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + sqlok);
//                    Console.WriteLine("1延迟5秒");
//                    DateTime dt1 = DateTime.Now;
//                    while ((DateTime.Now - dt1).TotalMilliseconds <500)
//                    {
//                        continue;
//                    };
//                    Console.WriteLine("2延迟5秒");
//                    WriteLogs("log.txt", "RunLog", "ReadWaitTime2");
//                    //ReadWaitTime2(null, null);

//                }

//            }
//            AllYeild.Clear(); //清除数组列表

//            }
//            catch (Exception ex)
//            {
//                WriteLogs("log.txt", "Exception:", ex.Message.ToString());

//            }
//        }


//        //读字-待料时间采集---2
//        private void ReadWaitTime2(object sender, RoutedEventArgs e)
//        {
//            Console.WriteLine("读ReadWaitTime");
//            ArrayList AllYeild = new ArrayList();
//            try
//            {
//                string[] res;
//            string str1 = "14500";// writTime.Text;
//            string conutNB = "48";// writTime_count.Text;
//            if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(conutNB))
//            {
//                MessageBox.Show("地址和不能数量为空！", "连接失败");
//            }
//            else
//            {
//                omron.ReadDWords(int.Parse(str1), int.Parse(conutNB), out res);
//                    DateTime dt13 = DateTime.Now;
//                    while ((DateTime.Now - dt13).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    // 输出d地址
//                    //ValueBox.AppendText("读字:" + int.Parse(str1) + "=> ");
//                    int i = 0;
//                int n = 0;
//                int m = 0;
//                var nStr = "";
//                var mStr = "";
//                Console.WriteLine("待料时间res:", res, "res.length:", res.Length);
//                if (res.Length < 1)
//                {
//                    Console.WriteLine("待料res数据不对:", res);
//                        WriteLogs("log.txt", "RunLog", "待料res数据不对");
//                    }
//                else
//                {
//                    foreach (string item in res)
//                    {
//                        //ValueBox.AppendText(item);
//                        //ValueBox.AppendText(" ");//添加空格
//                        var hex2To10 = Convert.ToInt32(res[i], 16);
//                        if (n == 0)
//                        {
//                            nStr = item;
//                            n++;
//                        }
//                        else
//                        {
//                            mStr = nStr + item;
//                            n = 0;
//                            //Console.WriteLine("Mstr:" + mStr);
//                            var hexTo10 = Convert.ToInt32(mStr, 16);
//                            AllYeild.Add(hexTo10);
//                            //Console.WriteLine("hexTo10:" + hexTo10);
//                            m++;
//                        }
//                        //System.Diagnostics.Debug.WriteLine("item+res:" + item + "-" + res[i]);
//                        i++;
//                    }
//                    System.Diagnostics.Debug.WriteLine("Allyeild待料:" + AllYeild[0]);
//                    //ValueBox.AppendText("\n");
//                    Console.WriteLine("AllYeild.length:" + AllYeild.Count); //使用数量
//                    string type = "待料时间(分钟)";
//                    DateTime dt1 = DateTime.Now;
//                    while ((DateTime.Now - dt1).TotalMilliseconds <500)
//                    {
//                        continue;
//                    };

//                    WriteLogs("log.txt", "RunLog", "InsertDB2");
//                    InsertDB2(DBtable, dayAddress, AllYeild, type);
//                }
//            }

//                //Console.WriteLine("AllYeild.length:" + AllYeild.Capacity);//容器容量

//            }
//            catch (Exception ex)
//            {
//                WriteLogs("log.txt", "Exception:", ex.Message.ToString());
//            }
//        }


//        //数据库操作-待料时间储存----2
//        private void InsertDB2(string[] DBtable, string dayAddress, ArrayList AllYeild, string type)
//        {
//            try
//            {
//                string ldclass;
//            DateTime dt = DateTime.Now;
//            string dt24 = dt.ToString("HH");
//            string DT24 = dt.ToString("yyyyMMdd");
//            //DateTime.Parse(DT24).ToString("yyyyMMdd");
//            System.Diagnostics.Debug.WriteLine("时间:" + dt24 + "/" + int.Parse(dt24) + "/" + DT24);
//            string today = dt.ToString("yyyyMMdd");
//            if (int.Parse(dt24) < 8 || int.Parse(dt24) >= 20)
//            {
//                //晚班待料数据
//                System.Diagnostics.Debug.WriteLine("班别：晚班" + dt24);
//                ldclass = "晚班";

//                if (int.Parse(dt24) < 8 && int.Parse(dt24) >= 0) //晚班时间减1判断
//                {
//                    System.Diagnostics.Debug.WriteLine("日期DT24：" + DT24);
//                    DT24 = (int.Parse(DT24) - 1).ToString();
//                    System.Diagnostics.Debug.WriteLine("日期DT24-1：" + DT24);
//                }
//                else
//                {
//                    System.Diagnostics.Debug.WriteLine("dt24" + dt24 + "DT24:" + DT24);
//                }

//                string SqlStatus = (DbHelperSQL.GetNightMaxID("id", "dayyeild", DT24, type)).ToString();
//                    DateTime dt13 = DateTime.Now;
//                    while ((DateTime.Now - dt13).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    Console.WriteLine("SqlStatus0:" + SqlStatus);
//                string strSql;
//                if (int.Parse(SqlStatus) <= 1)
//                {
//                    //晚班待料数据新增1条
//                    Console.WriteLine("查询晚班数据条不存在" + SqlStatus);
//                    strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[40] + "','" + AllYeild[42] + "','" + AllYeild[44] + "','" + AllYeild[46] + "','" + AllYeild[0] + "','" + AllYeild[2] + "','" + AllYeild[4] + "','" + AllYeild[6] + "','" + AllYeild[8] + "','" + AllYeild[10] + "','" + AllYeild[12] + "','" + AllYeild[14] + "','" + ldclass + "','" + type + "')";   //+ "9999" +"','" 
//                    Console.WriteLine("strSql0:" + strSql);
//                }
//                else
//                {
//                    //晚班待料数据更新1条
//                    Console.WriteLine("查询数据条已经存在" + SqlStatus);
//                    strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[40] + ",H0910=" + AllYeild[42] + ",H1011=" + AllYeild[44] + ",H1112=" + AllYeild[46] + ",H1213=" + AllYeild[0] + ",H1314=" + AllYeild[2] + ",H1415=" + AllYeild[4] + ",H1516=" + AllYeild[6] + ",H1617=" + AllYeild[8] + ",H1718=" + AllYeild[10] + ",H1819=" + AllYeild[12] + ",H1920=" + AllYeild[14] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus;
//                    Console.WriteLine("strSql1:" + strSql);
//                }

//                //Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));
//                int sqlok = DbHelperSQL.ExecuteSql(strSql);
//                    DateTime dt23 = DateTime.Now;
//                    while ((DateTime.Now - dt23).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    if (sqlok == 1 && run == true)
//                {
//                    Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + sqlok);
//                    //ReadBugTime3(null, null);
//                    WriteLogs("log.txt", "RunLog", "ReadBugTime3");
//                }

//            }
//            else
//            {
//                ldclass = "白班";
//                System.Diagnostics.Debug.WriteLine("班别：" + ldclass);
//                string SqlStatus1 = (DbHelperSQL.GetDayMaxID("id", "dayyeild", today, type)).ToString();
//                    DateTime dt23 = DateTime.Now;
//                    while ((DateTime.Now - dt23).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    Console.WriteLine("SqlStatus1:" + SqlStatus1);
//                string strSql;//白班数据库操作膜版
//                if (int.Parse(SqlStatus1) <= 1)
//                {
//                    //白班待料数据新增1条
//                    Console.WriteLine("查询数据条不存在" + SqlStatus1);
//                    strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[16] + "','" + AllYeild[18] + "','" + AllYeild[20] + "','" + AllYeild[22] + "','" + AllYeild[24] + "','" + AllYeild[26] + "','" + AllYeild[28] + "','" + AllYeild[30] + "','" + AllYeild[32] + "','" + AllYeild[34] + "','" + AllYeild[36] + "','" + AllYeild[38] + "','" + ldclass + "','" + type + "')";
//                    Console.WriteLine("strSql1:" + strSql);

//                }
//                else
//                {
//                    //白班待料数据更新1条
//                    Console.WriteLine("查询数据条已经存在" + SqlStatus1);
//                    strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[16] + ",H0910=" + AllYeild[18] + ",H1011=" + AllYeild[20] + ",H1112=" + AllYeild[22] + ",H1213=" + AllYeild[24] + ",H1314=" + AllYeild[26] + ",H1415=" + AllYeild[28] + ",H1516=" + AllYeild[30] + ",H1617=" + AllYeild[32] + ",H1718=" + AllYeild[34] + ",H1819=" + AllYeild[36] + ",H1920=" + AllYeild[38] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus1;
//                    Console.WriteLine("strSql1:" + strSql);
//                }
//                //Console.WriteLine("DbHelperSQL数据操作1,1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));
//                    int sqlok = DbHelperSQL.ExecuteSql(strSql);
//                    DateTime dt25 = DateTime.Now;
//                    while ((DateTime.Now - dt25).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    if (sqlok == 1 && run == true)
//                {
//                    Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + sqlok);
//                    WriteLogs("log.txt", "RunLog", "ReadBugTime3");
//                    //ReadBugTime3(null, null);

//                }
//            }
//            AllYeild.Clear(); //清除数组列表
//            }
//            catch (Exception e)
//            {
//                WriteLogs("log.txt", "Exception:", e.Message.ToString());
//            }

//        }


//        //读字-故障时间集---3
//        private void ReadBugTime3(object sender, RoutedEventArgs e)
//        {
//            ArrayList AllYeild = new ArrayList();
//            try
//            {
//                string[] res;
//                string str1 = "14550";// bugTime.Text;
//                string conutNB = "48";// bugTime_count.Text;
//                if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(conutNB))
//                {
//                    MessageBox.Show("地址和不能数量为空！", "连接失败");
//                }
//                else
//                {
//                    omron.ReadDWords(int.Parse(str1), int.Parse(conutNB), out res);
//                    DateTime dt31 = DateTime.Now;
//                    while ((DateTime.Now - dt31).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    // 输出d地址
//                    //ValueBox.AppendText("读字:" + int.Parse(str1) + "=> ");

//                    int i = 0;
//                    int n = 0;
//                    int m = 0;
//                    var nStr = "";
//                    var mStr = "";
//                    Console.WriteLine("故障时间res:" + res + "res.length:" + res.Length);
//                    if (res.Length < 1)
//                    {
//                        Console.WriteLine("故障时间res数据不对:", res);
//                        WriteLogs("log.txt", "待料","待料res数据不对");
//                    }
//                    else
//                    {
//                        foreach (string item in res)
//                        {
//                            //ValueBox.AppendText(item);
//                            //ValueBox.AppendText(" ");//添加空格
//                            var hex2To10 = Convert.ToInt32(res[i], 16);
//                            if (n == 0)
//                            {
//                                nStr = item;
//                                n++;
//                            }
//                            else
//                            {
//                                mStr = nStr + item;
//                                n = 0;
//                                //Console.WriteLine("Mstr:" + mStr);
//                                var hexTo10 = Convert.ToInt32(mStr, 16);
//                                AllYeild.Add(hexTo10);
//                                //Console.WriteLine("hexTo10:" + hexTo10);
//                                m++;
//                            }

//                            i++;
//                        }
//                    }
//                    System.Diagnostics.Debug.WriteLine("Allyeild故障时间:" + AllYeild[0]);
//                    //System.Diagnostics.Debug.WriteLine("Allyeild故障时间：" + res[0]);
//                    Console.WriteLine("AllYeild.length:" + AllYeild.Count); //使用数量                                                        //Console.WriteLine("AllYeild.length:" + AllYeild.Capacity);//容器容量
//                    //ValueBox.AppendText("\n");
//                    //报存数据
//                    DateTime dt1 = DateTime.Now;
//                    while ((DateTime.Now - dt1).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    string type = "故障时间(分钟)";

//                    WriteLogs("log.txt", "RunLog", "InsertDB3");
//                    InsertDB3(DBtable, dayAddress, AllYeild, type);

//                }

//        }
//            catch (Exception ex)
//            {
//                WriteLogs("log.txt", "Exception:", ex.Message.ToString());
//            }
//}


//        //数据库操作-故障时间储存----3
//        private void InsertDB3(string[] DBtable, string dayAddress, ArrayList AllYeild, string type)
//        {
//            try
//            {
//                string ldclass;
//                DateTime dt = DateTime.Now;
//                string dt24 = dt.ToString("HH");
//                string DT24 = dt.ToString("yyyyMMdd");
//                //DateTime.Parse(DT24).ToString("yyyyMMdd");
//                System.Diagnostics.Debug.WriteLine("时间:" + dt24 + "/" + int.Parse(dt24) + "/" + DT24);
//                string today = dt.ToString("yyyyMMdd");
//                if (int.Parse(dt24) < 8 || int.Parse(dt24) >= 20)
//                {
//                    //晚班故障数据
//                    System.Diagnostics.Debug.WriteLine("班别：晚班" + dt24);
//                    ldclass = "晚班";

//                    if (int.Parse(dt24) < 8 && int.Parse(dt24) >= 0) //晚班时间减1判断
//                    {
//                        System.Diagnostics.Debug.WriteLine("日期DT24：" + DT24);
//                        DT24 = (int.Parse(DT24) - 1).ToString();
//                        System.Diagnostics.Debug.WriteLine("日期DT24-1：" + DT24);
//                    }
//                    else
//                    {
//                        System.Diagnostics.Debug.WriteLine("dt24" + dt24 + "DT24:" + DT24);
//                    }

//                    string SqlStatus = (DbHelperSQL.GetNightMaxID("id", "dayyeild", DT24, type)).ToString();//DT24是根据20-24点处理后的时间
//                    Console.WriteLine("SqlStatus0:" + SqlStatus);
//                    DateTime dt13 = DateTime.Now;
//                    while ((DateTime.Now - dt13).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    string strSql;
//                    if (int.Parse(SqlStatus) <= 1)
//                    {
//                        //晚班故障数据新增1条
//                        Console.WriteLine("查询晚班数据条不存在" + SqlStatus);
//                        strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[40] + "','" + AllYeild[42] + "','" + AllYeild[44] + "','" + AllYeild[46] + "','" + AllYeild[0] + "','" + AllYeild[2] + "','" + AllYeild[4] + "','" + AllYeild[6] + "','" + AllYeild[8] + "','" + AllYeild[10] + "','" + AllYeild[12] + "','" + AllYeild[14] + "','" + ldclass + "','" + type + "')";   //+ "9999" +"','" 
//                        Console.WriteLine("strSql0:" + strSql);
//                    }
//                    else
//                    {
//                        //晚班故障数据更新1条
//                        Console.WriteLine("查询数据条已经存在" + SqlStatus);
//                        strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[40] + ",H0910=" + AllYeild[42] + ",H1011=" + AllYeild[44] + ",H1112=" + AllYeild[46] + ",H1213=" + AllYeild[0] + ",H1314=" + AllYeild[2] + ",H1415=" + AllYeild[4] + ",H1516=" + AllYeild[6] + ",H1617=" + AllYeild[8] + ",H1718=" + AllYeild[10] + ",H1819=" + AllYeild[12] + ",H1920=" + AllYeild[14] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus;
//                        Console.WriteLine("strSql1:" + strSql);
//                    }


//                    //Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));

//                    int sqlok = DbHelperSQL.ExecuteSql(strSql);
//                    DateTime dt33 = DateTime.Now;
//                    while ((DateTime.Now - dt33).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    if (sqlok == 1 && run == true)
//                    {
//                        Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + sqlok);
//                        DateTime dt1 = DateTime.Now;
//                        while ((DateTime.Now - dt1).TotalMilliseconds < 500)
//                        {
//                            continue;
//                        };                     
//                        WriteLogs("log.txt", "RunLog", "ReadZuopao4");
//                        //ReadZuopao4(null, null);

//                    }
//                }
//                else
//                {
//                    ldclass = "白班";
//                    System.Diagnostics.Debug.WriteLine("班别：" + ldclass);
//                    string SqlStatus1 = (DbHelperSQL.GetDayMaxID("id", "dayyeild", today, type)).ToString(); //today是今天日期
//                    DateTime dt33 = DateTime.Now;
//                    while ((DateTime.Now - dt33).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    Console.WriteLine("SqlStatus1:" + SqlStatus1);
//                    string strSql;//白班数据库操作膜版
//                    if (int.Parse(SqlStatus1) <= 1)
//                    {
//                        //白班故障数据新增1条
//                        Console.WriteLine("查询数据条不存在" + SqlStatus1);
//                        strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[16] + "','" + AllYeild[18] + "','" + AllYeild[20] + "','" + AllYeild[22] + "','" + AllYeild[24] + "','" + AllYeild[26] + "','" + AllYeild[28] + "','" + AllYeild[30] + "','" + AllYeild[32] + "','" + AllYeild[34] + "','" + AllYeild[36] + "','" + AllYeild[38] + "','" + ldclass + "','" + type + "')";
//                        Console.WriteLine("strSql1:" + strSql);
//                    }
//                    else
//                    {
//                        //白班故障数据更新1条
//                        Console.WriteLine("查询数据条已经存在" + SqlStatus1);
//                        strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[16] + ",H0910=" + AllYeild[18] + ",H1011=" + AllYeild[20] + ",H1112=" + AllYeild[22] + ",H1213=" + AllYeild[24] + ",H1314=" + AllYeild[26] + ",H1415=" + AllYeild[28] + ",H1516=" + AllYeild[30] + ",H1617=" + AllYeild[32] + ",H1718=" + AllYeild[34] + ",H1819=" + AllYeild[36] + ",H1920=" + AllYeild[38] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus1;
//                        Console.WriteLine("strSql1:" + strSql);
//                    }
//                    //Console.WriteLine("DbHelperSQL数据操作3,1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));

//                    int sqlok = DbHelperSQL.ExecuteSql(strSql);
//                    DateTime dt13 = DateTime.Now;
//                    while ((DateTime.Now - dt13).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    if (sqlok == 1 && run == true)
//                    {
//                        Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + sqlok);
//                        DateTime dt1 = DateTime.Now;

//                        WriteLogs("log.txt", "RunLog", "ReadZuopao4");
//                        //ReadZuopao4(null, null);
//                    }
//                }
//                AllYeild.Clear(); //清除数组列表
//            }
//            catch (Exception e)
//            {
//                WriteLogs("log.txt", "RunLog", e.Message);
//                throw e;
//            }

//        }


//        //读字-左抛膜次数采集---4
//        private void ReadZuopao4(object sender, RoutedEventArgs e)
//        {
//            ArrayList AllYeild = new ArrayList();
//            try
//            {
//                string[] res;
//                string str1 = "14700";// zuopao.Text;
//                string conutNB = "48";// zuopao_count.Text;
//                if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(conutNB))
//                {
//                    MessageBox.Show("地址和不能数量为空！", "连接失败");
//                }
//                else
//                {
//                    omron.ReadDWords(int.Parse(str1), int.Parse(conutNB), out res);
//                    DateTime dt13 = DateTime.Now;
//                    while ((DateTime.Now - dt13).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    // 输出d地址
//                    //ValueBox.AppendText("读字:" + int.Parse(str1) + "=> ");

//                    int i = 0;
//                    int n = 0;
//                    int m = 0;
//                    var nStr = "";
//                    var mStr = "";
//                    Console.WriteLine("左边res:" + res + "res.length:" + res.Length);
//                    if (res.Length < 1)
//                    {
//                        Console.WriteLine("左边res数据不对:", res);
//                        WriteLogs("log.txt", "待料", "待料res数据不对");
//                    }
//                    else
//                    {
//                        foreach (string item in res)
//                        {
//                            //ValueBox.AppendText(item);
//                            //ValueBox.AppendText(" ");//添加空格
//                            var hex2To10 = Convert.ToInt32(res[i], 16);
//                            if (n == 0)
//                            {
//                                nStr = item;
//                                n++;
//                            }
//                            else
//                            {
//                                mStr = nStr + item;
//                                n = 0;
//                                //Console.WriteLine("Mstr:" + mStr);
//                                var hexTo10 = Convert.ToInt32(mStr, 16);
//                                AllYeild.Add(hexTo10);
//                                //Console.WriteLine("hexTo10:" + hexTo10);
//                                m++;
//                            }
//                            //System.Diagnostics.Debug.WriteLine("item+res:" + item + "-" + res[i]);
//                            i++;
//                        }
//                    }
//                    System.Diagnostics.Debug.WriteLine("Allyeild左抛次数:" + AllYeild[0]);
//                    Console.WriteLine("AllYeild.length:" + AllYeild.Count); //使用数量
//                                                                            //Console.WriteLine("AllYeild.length:" + AllYeild.Capacity);//容器容量
//                    //ValueBox.AppendText("\n");
//                    DateTime dt1 = DateTime.Now;
//                    while ((DateTime.Now - dt1).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    //报存数据
//                    string type = "左抛膜(pcs)";

//                    WriteLogs("log.txt", "RunLog", "InsertDB4");
//                    InsertDB4(DBtable, dayAddress, AllYeild, type);

//                }

//        }
//            catch (Exception ex)
//            {
//                WriteLogs("log.txt", "Exception:", ex.Message.ToString());
//            }
//        }


//        //数据库操作-左抛膜次数储存----4
//        private void InsertDB4(string[] DBtable, string dayAddress, ArrayList AllYeild, string type)
//        {
//            try
//            {
//                string ldclass;
//                DateTime dt = DateTime.Now;
//                string dt24 = dt.ToString("HH");
//                string DT24 = dt.ToString("yyyyMMdd");
//                //DateTime.Parse(DT24).ToString("yyyyMMdd");
//                System.Diagnostics.Debug.WriteLine("时间:" + dt24 + "/" + int.Parse(dt24) + "/" + DT24);
//                string today = dt.ToString("yyyyMMdd");
//                if (int.Parse(dt24) < 8 || int.Parse(dt24) >= 20)
//                {
//                    //晚班左边抛膜数据
//                    System.Diagnostics.Debug.WriteLine("班别：晚班" + dt24);
//                    ldclass = "晚班";

//                    if (int.Parse(dt24) < 8 && int.Parse(dt24) >= 0) //晚班时间减1判断
//                    {
//                        System.Diagnostics.Debug.WriteLine("日期DT24：" + DT24);
//                        DT24 = (int.Parse(DT24) - 1).ToString();
//                        System.Diagnostics.Debug.WriteLine("日期DT24-1：" + DT24);
//                    }
//                    else
//                    {
//                        System.Diagnostics.Debug.WriteLine("dt24" + dt24 + "DT24:" + DT24);
//                    }

//                    string SqlStatus = (DbHelperSQL.GetNightMaxID("id", "dayyeild", DT24, type)).ToString();
//                    DateTime dt13 = DateTime.Now;
//                    while ((DateTime.Now - dt13).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    Console.WriteLine("SqlStatus0:" + SqlStatus);
//                    string strSql;
//                    if (int.Parse(SqlStatus) <= 1)
//                    {
//                        //晚班左边数据新增1条
//                        Console.WriteLine("查询晚班数据条不存在" + SqlStatus);
//                        strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[40] + "','" + AllYeild[42] + "','" + AllYeild[44] + "','" + AllYeild[46] + "','" + AllYeild[0] + "','" + AllYeild[2] + "','" + AllYeild[4] + "','" + AllYeild[6] + "','" + AllYeild[8] + "','" + AllYeild[10] + "','" + AllYeild[12] + "','" + AllYeild[14] + "','" + ldclass + "','" + type + "')";   //+ "9999" +"','" 
//                        Console.WriteLine("strSql0:" + strSql);
//                    }
//                    else
//                    {
//                        //晚班左边抛膜数据更新1条
//                        Console.WriteLine("查询数据条已经存在" + SqlStatus);
//                        strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[40] + ",H0910=" + AllYeild[42] + ",H1011=" + AllYeild[44] + ",H1112=" + AllYeild[46] + ",H1213=" + AllYeild[0] + ",H1314=" + AllYeild[2] + ",H1415=" + AllYeild[4] + ",H1516=" + AllYeild[6] + ",H1617=" + AllYeild[8] + ",H1718=" + AllYeild[10] + ",H1819=" + AllYeild[12] + ",H1920=" + AllYeild[14] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus;
//                        Console.WriteLine("strSql1:" + strSql);
//                    }


//                    //Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));
//                    int sqlok = DbHelperSQL.ExecuteSql(strSql);
//                    DateTime dt43 = DateTime.Now;
//                    while ((DateTime.Now - dt43).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };
//                    if (sqlok == 1 && run == true)
//                    {
//                        Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + sqlok);
//                        WriteLogs("log.txt", "RunLog", "ReadYoupao5");
//                        //ReadYoupao5(null, null);
//                    }
//                }
//                else
//                {
//                    ldclass = "白班";
//                    System.Diagnostics.Debug.WriteLine("班别：" + ldclass);
//                    string SqlStatus1 = (DbHelperSQL.GetDayMaxID("id", "dayyeild", today, type)).ToString();//查询
//                    DateTime dt44 = DateTime.Now;
//                    while ((DateTime.Now - dt44).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };

//                    Console.WriteLine("SqlStatus1:" + SqlStatus1);
//                    string strSql;//白班数据库操作膜版
//                    if (int.Parse(SqlStatus1) <= 1)
//                    {
//                        //白班左边抛膜数据新增1条
//                        Console.WriteLine("查询数据条不存在" + SqlStatus1);
//                        strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[16] + "','" + AllYeild[18] + "','" + AllYeild[20] + "','" + AllYeild[22] + "','" + AllYeild[24] + "','" + AllYeild[26] + "','" + AllYeild[28] + "','" + AllYeild[30] + "','" + AllYeild[32] + "','" + AllYeild[34] + "','" + AllYeild[36] + "','" + AllYeild[38] + "','" + ldclass + "','" + type + "')";
//                        Console.WriteLine("strSql1:" + strSql);

//                    }
//                    else
//                    {
//                        //白班左边抛膜数据更新1条
//                        Console.WriteLine("查询数据条已经存在" + SqlStatus1);
//                        strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[16] + ",H0910=" + AllYeild[18] + ",H1011=" + AllYeild[20] + ",H1112=" + AllYeild[22] + ",H1213=" + AllYeild[24] + ",H1314=" + AllYeild[26] + ",H1415=" + AllYeild[28] + ",H1516=" + AllYeild[30] + ",H1617=" + AllYeild[32] + ",H1718=" + AllYeild[34] + ",H1819=" + AllYeild[36] + ",H1920=" + AllYeild[38] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus1;
//                        Console.WriteLine("strSql1:" + strSql);
//                    }
//                    //Console.WriteLine("DbHelperSQL数据操作3,1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));
//                    DateTime dt45 = DateTime.Now;
//                    while ((DateTime.Now - dt45).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };

//                    int sqlok = DbHelperSQL.ExecuteSql(strSql);
//                    if (sqlok == 1 && run == true)
//                    {
//                        Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + sqlok);
//                        WriteLogs("log.txt", "RunLog", "ReadYoupao5");
//                        //ReadYoupao5(null, null);

//                    }
//                }
//                AllYeild.Clear(); //清除数组列表
//            }
//            catch (Exception e)
//            {
//                WriteLogs("log.txt", "Exception:", e.Message.ToString());
//            }

//        }


//        //读字-右抛膜次数采集---5
//        private void ReadYoupao5(object sender, RoutedEventArgs e)
//        {
//            ArrayList AllYeild = new ArrayList();
//            try
//            {
//                string[] res;
//            string str1 = "14800";//youpao.Text;
//            string conutNB = "48";// youpao_count.Text;
//            if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(conutNB))
//            {
//                MessageBox.Show("地址和不能数量为空！", "连接失败");
//            }
//            else
//            {
//                omron.ReadDWords(int.Parse(str1), int.Parse(conutNB), out res);
//                    DateTime dt51 = DateTime.Now;
//                    while ((DateTime.Now - dt51).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };

//                    // 输出d地址
//                    //ValueBox.AppendText("读字:" + int.Parse(str1) + "=> ");
//                    int i = 0;
//                int n = 0;
//                int m = 0;
//                var nStr = "";
//                var mStr = "";
//                Console.WriteLine("右边抛膜res:" + res + "res.length:" + res.Length);
//                if (res.Length < 1)
//                {
//                    Console.WriteLine("右边抛膜res数据不对:", res);
//                        WriteLogs("log.txt", "待料", "待料res数据不对");
//                    }
//                else
//                {
//                    foreach (string item in res)
//                    {
//                        //ValueBox.AppendText(item);
//                        //ValueBox.AppendText(" ");//添加空格
//                        var hex2To10 = Convert.ToInt32(res[i], 16);
//                        if (n == 0)
//                        {
//                            nStr = item;
//                            n++;
//                        }
//                        else
//                        {
//                            mStr = nStr + item;
//                            n = 0;
//                            //Console.WriteLine("Mstr:" + mStr);
//                            var hexTo10 = Convert.ToInt32(mStr, 16);
//                            AllYeild.Add(hexTo10);
//                            //Console.WriteLine("hexTo10:" + hexTo10);
//                            m++;
//                        }
//                        //System.Diagnostics.Debug.WriteLine("item+res:" + item + "-" + res[i]);
//                        i++;
//                    }
//                }
//                System.Diagnostics.Debug.WriteLine("Allyeild右抛次数:" + AllYeild[0]);
//                Console.WriteLine("AllYeild.length:" + AllYeild.Count); //使用数量
//                                                                        //Console.WriteLine("AllYeild.length:" + AllYeild.Capacity);//容器容量
//                //ValueBox.AppendText("\n");
//                DateTime dt1 = DateTime.Now;
//                while ((DateTime.Now - dt1).TotalMilliseconds < 500)
//                {
//                    continue;
//                };
//                //报存数据
//                string type = "右抛膜(pcs)";
//                WriteLogs("log.txt", "RunLog", "InsertDB5");
//                InsertDB5(DBtable, dayAddress, AllYeild, type);


//            }

//            }
//            catch (Exception ex)
//            {
//                WriteLogs("log.txt", "Exception:", ex.Message.ToString());
//            }
//        }


//        //数据库操作-右抛膜次数储存----5
//        private void InsertDB5(string[] DBtable, string dayAddress, ArrayList AllYeild, string type)
//        {
//            try
//            {
//                string ldclass;
//                DateTime dt = DateTime.Now;
//                string dt24 = dt.ToString("HH");
//                string DT24 = dt.ToString("yyyyMMdd");
//                //DateTime.Parse(DT24).ToString("yyyyMMdd");
//                System.Diagnostics.Debug.WriteLine("时间:" + dt24 + "/" + int.Parse(dt24) + "/" + DT24);
//                string today = dt.ToString("yyyyMMdd");
//                if (int.Parse(dt24) < 8 || int.Parse(dt24) >= 20)//小于大于20点8点或者
//                {
//                    //晚班右边抛膜数据
//                    System.Diagnostics.Debug.WriteLine("班别：晚班" + dt24);
//                    ldclass = "晚班";

//                    if (int.Parse(dt24) < 8 && int.Parse(dt24) >= 0) //晚班时间减1判断
//                    {
//                        System.Diagnostics.Debug.WriteLine("日期DT24：" + DT24);
//                        DT24 = (int.Parse(DT24) - 1).ToString();
//                        System.Diagnostics.Debug.WriteLine("日期DT24-1：" + DT24);
//                    }
//                    else
//                    {
//                        System.Diagnostics.Debug.WriteLine("dt24" + dt24 + "DT24:" + DT24);
//                    }

//                    string SqlStatus = (DbHelperSQL.GetNightMaxID("id", "dayyeild", DT24, type)).ToString();//查询
//                    DateTime dt53 = DateTime.Now;
//                    while ((DateTime.Now - dt53).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };

//                    Console.WriteLine("SqlStatus0:" + SqlStatus);
//                    string strSql;
//                    if (int.Parse(SqlStatus) <= 1)
//                    {
//                        //晚班晚班右边抛膜数据新增1条
//                        Console.WriteLine("查询晚班数据条不存在" + SqlStatus);
//                        strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[40] + "','" + AllYeild[42] + "','" + AllYeild[44] + "','" + AllYeild[46] + "','" + AllYeild[0] + "','" + AllYeild[2] + "','" + AllYeild[4] + "','" + AllYeild[6] + "','" + AllYeild[8] + "','" + AllYeild[10] + "','" + AllYeild[12] + "','" + AllYeild[14] + "','" + ldclass + "','" + type + "')";   //+ "9999" +"','" 
//                        Console.WriteLine("strSql0:" + strSql);
//                    }
//                    else
//                    {
//                        //晚班右边抛膜数据更新1条
//                        Console.WriteLine("查询数据条已经存在" + SqlStatus);
//                        strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[40] + ",H0910=" + AllYeild[42] + ",H1011=" + AllYeild[44] + ",H1112=" + AllYeild[46] + ",H1213=" + AllYeild[0] + ",H1314=" + AllYeild[2] + ",H1415=" + AllYeild[4] + ",H1516=" + AllYeild[6] + ",H1617=" + AllYeild[8] + ",H1718=" + AllYeild[10] + ",H1819=" + AllYeild[12] + ",H1920=" + AllYeild[14] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus;
//                        Console.WriteLine("strSql1:" + strSql);
//                    }
//                    //Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));
//                    int sqlok = DbHelperSQL.ExecuteSql(strSql);//保存数据
//                    DateTime dt54 = DateTime.Now;
//                    while ((DateTime.Now - dt54).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };

//                    if (sqlok == 1 && run == true)
//                    {
//                        Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + sqlok);

//                        WriteLogs("log.txt", "RunLog", "尾部重调用ReadBtn_Click");
//                        //ReadBtn_Click(null, null);

//                    }
//                }
//                else
//                {
//                    ldclass = "白班";
//                    System.Diagnostics.Debug.WriteLine("班别：" + ldclass);
//                    string SqlStatus1 = (DbHelperSQL.GetDayMaxID("id", "dayyeild", today, type)).ToString();//查询
//                    DateTime dt54 = DateTime.Now;
//                    while ((DateTime.Now - dt54).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };

//                    Console.WriteLine("SqlStatus1:" + SqlStatus1);
//                    string strSql;//白班数据库操作膜版
//                    if (int.Parse(SqlStatus1) <= 1)
//                    {
//                        //白班右抛膜数据新增1条
//                        Console.WriteLine("查询数据条不存在" + SqlStatus1);
//                        strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[16] + "','" + AllYeild[18] + "','" + AllYeild[20] + "','" + AllYeild[22] + "','" + AllYeild[24] + "','" + AllYeild[26] + "','" + AllYeild[28] + "','" + AllYeild[30] + "','" + AllYeild[32] + "','" + AllYeild[34] + "','" + AllYeild[36] + "','" + AllYeild[38] + "','" + ldclass + "','" + type + "')";
//                        Console.WriteLine("strSql1:" + strSql);

//                    }
//                    else
//                    {
//                        //白班右抛膜数据更新1条
//                        Console.WriteLine("查询数据条已经存在" + SqlStatus1);
//                        strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[16] + ",H0910=" + AllYeild[18] + ",H1011=" + AllYeild[20] + ",H1112=" + AllYeild[22] + ",H1213=" + AllYeild[24] + ",H1314=" + AllYeild[26] + ",H1415=" + AllYeild[28] + ",H1516=" + AllYeild[30] + ",H1617=" + AllYeild[32] + ",H1718=" + AllYeild[34] + ",H1819=" + AllYeild[36] + ",H1920=" + AllYeild[38] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus1;
//                        Console.WriteLine("strSql1:" + strSql);
//                    }
//                    //Console.WriteLine("DbHelperSQL数据操作4,1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));

//                    int sqlok = DbHelperSQL.ExecuteSql(strSql);//查询是否存在
//                    DateTime dt44 = DateTime.Now;
//                    while ((DateTime.Now - dt44).TotalMilliseconds < 500)
//                    {
//                        continue;
//                    };

//                    if (sqlok == 1 && run == true)
//                    {
//                        Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + sqlok);
//                        DateTime dt1 = DateTime.Now;
//                        while ((DateTime.Now - dt1).TotalMilliseconds < 1000)
//                        {
//                            continue;
//                        }; 
//                        WriteLogs("log.txt", "RunLog", "尾部重调用ReadBtn_Click");
//                        //ReadBtn_Click(null, null);
//                    }
//                }
//                AllYeild.Clear(); //清除数组列表
//            }
//            catch (Exception e)
//            {
//                WriteLogs("log.txt", "Exception:", e.Message.ToString());
//            }

//        }


//        //写出日志
//        public static void WriteLogs(string fileName, string type, string content)
//        {
//            string path = AppDomain.CurrentDomain.BaseDirectory;
//            if (!string.IsNullOrEmpty(path))
//            {
//                path = AppDomain.CurrentDomain.BaseDirectory + fileName;
//                if (!Directory.Exists(path))
//                {
//                    Directory.CreateDirectory(path);
//                }
//                path = path + "\\" + DateTime.Now.ToString("yyyyMMdd");
//                if (!Directory.Exists(path))
//                {
//                    Directory.CreateDirectory(path);
//                }
//                path = path + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
//                if (!File.Exists(path))
//                {
//                    FileStream fs = File.Create(path);
//                    fs.Close();
//                }
//                if (File.Exists(path))
//                {
//                    StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default);
//                    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") +"--"+ type + "-->" + content);
//                    sw.WriteLine("----------------------------------------");
//                    sw.Flush();
//                    sw.Close();
//                    //Console.WriteLine("yyyy-MM-dd HH:mm:ss.fff" + type + "-->" + content + path);
//                }
//                //Console.WriteLine("yyyy-MM-dd HH:mm:ss.fff" + type + "-->" + content + path);
//            }
//        }


//         //函数循环定时器函数
//        public void theout(object source, System.Timers.ElapsedEventArgs e)

//        {
//            InitializeComponent();
//            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//设置该属性 为false
//            //MessageBox.Show("OK!")运行;
//            Console.WriteLine("定时器运行");

//            ReadBugTime3(null, null);
//            DateTime dt1 = DateTime.Now;
//            while ((DateTime.Now - dt1).TotalMilliseconds < 1000)
//            {
//                continue;
//            };

//            ReadWaitTime2(null, null);
//            DateTime dt11 = DateTime.Now;
//            while ((DateTime.Now - dt11).TotalMilliseconds < 1000)
//            {
//                continue;
//            };

//            ReadYoupao5(null, null);
//            DateTime dt111 = DateTime.Now;
//            while ((DateTime.Now - dt111).TotalMilliseconds < 1000)
//            {
//                continue;
//            };

//            ReadZuopao4(null, null);
//            DateTime dt1111 = DateTime.Now;
//            while ((DateTime.Now - dt1111).TotalMilliseconds < 1000)
//            {
//                continue;
//            };
//            ReadBtn_Click(null, null);

//        }









//    }
//}





using OmronLib;
using System;
//using System.Collections.Generic;
//using System.Collections.ObjectModel;
//using System.Data;
//using System.Linq;
using System.Net;
//using System.Text;
//using System.Threading;
using System.Timers;
using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Data;
//using System.Windows.Documents;
//using System.Windows.Input;
using System.Windows.Media;
//using System.Windows.Media.Imaging;
//using System.Windows.Navigation;
//using System.Windows.Shapes;
using System.Collections;
//using System.Runtime.InteropServices;
//using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Diagnostics;
//using System.Threading.Tasks;


namespace OmronHelper
{
    /// MainWindow.xaml 的交互逻辑
    public partial class MainWindow : Window
    {
        private readonly System.Timers.Timer timer = new System.Timers.Timer(1000);//灯定时刷新
        private readonly OmronLib.Omron omron = new OmronLib.Omron();
        //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        object obj = new { Name = "lily", Age = 12, port = 9600, ip = "192.168.250.1" };

        //定时循环
        System.Timers.Timer t = new System.Timers.Timer(5000);//实例化Timer类，设置间隔时间为10000毫秒；

        //主入库函数
        bool run = false;
        string ip;
        string port;
        public MainWindow()
        {
            object ob = new { Port = 9600, ip = "192.168.250.1" };
            InitializeComponent();
            timer.Elapsed += Timer_Elapsed;
            timer.Start();

            port = PortBox.Text;
            ip = IPBox.Text;
            WriteLogs("log.txt", "RunLog", "主函数启动");

            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//设置该属性 为false

        }


        /// 用个timer来更新界面上的灯
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                StatusLight.Fill = omron.IsConnected ? new SolidColorBrush(Colors.LimeGreen) : new SolidColorBrush(Colors.Red);
            }));



        }


        /// 按钮手动连接
        public void ConnectBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(IPBox.Text) || string.IsNullOrEmpty(PortBox.Text))
                {
                    MessageBox.Show("IP和Port不能为空！", "连接失败");
                }
                else
                {
                    omron.IPAddr = IPAddress.Parse(IPBox.Text);
                    int port;
                    if (int.TryParse(PortBox.Text, out port))
                    {
                        omron.Port = port;
                    }

                    string info;
                    if (!omron.PlcConnect(out info))
                    {
                        MessageBox.Show(info, "连接失败");
                        return;
                    }
                    else
                    {
                        ReadBtn.IsEnabled = true;
                        ReadBitBtn.IsEnabled = true;
                        WriteBtn.IsEnabled = true;
                        ReadBtn_Copy.IsEnabled = true;
                        ReadBtn_Copy0.IsEnabled = true;
                        ReadBtn_Copy1.IsEnabled = true;
                        ReadBtn_Copy2.IsEnabled = true;
                        run = true;
                        Console.WriteLine("链接Plc成功");
                        DateTime dt1 = DateTime.Now;
                        StatusLight1.Fill = new SolidColorBrush(Colors.LimeGreen);
                        WriteLogs("log.txt", "RunLog", "手动链接PLC");

                        //定时循环
                        t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件；
                        t.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
                        t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                        t.Start();

                        //开新线程循环
                        //Program obj = new Program();
                        //Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString());
                        //Thread thread = new Thread(new ThreadStart(obj.Method1));
                        //thread.Start();
                    }
                }
            }
            catch (Exception ex)
            {
                WriteLogs("log.txt", "ConnectBtn_Click:", ex.Message.ToString());
            }
            return;
        }


        /// 断开连接
        private void DisconnectBtn_Click(object sender, RoutedEventArgs e)
        {
            omron.DisConnect();
            ReadBtn.IsEnabled = false;
            ReadBitBtn.IsEnabled = false;
            WriteBtn.IsEnabled = false;
            ReadBtn_Copy.IsEnabled = false;
            ReadBtn_Copy0.IsEnabled = false;
            ReadBtn_Copy1.IsEnabled = false;
            ReadBtn_Copy2.IsEnabled = false;
            StatusLight1.Fill = new SolidColorBrush(Colors.Red);
            run = false;
            WriteLogs("log.txt", "RunLog", "断开PLC");
            t.Enabled = false;//是否执行System.Timers.Timer.Elapsed事件；
            t.Stop();
            //MainWindow.Exit();
            //MainWindow.close();
            //MainWindow.Exit(exitCode);
            //DbHelperSQL.ExecuteScalar("eixt");
        }


        /// 读位
        private void ReadBitBtn_Click(object sender, RoutedEventArgs e)
        {
            // 从D8100.0读到D8100.15
            string[] res;

            omron.ReadDBits(4440, 0, 16, out res);

            // 输出
            ValueBox.AppendText("读位 D14440.0-D14440.15    ");


            foreach (string item in res)
            {
                ValueBox.AppendText(item);
                ValueBox.AppendText(" ");
            }
            ValueBox.AppendText("\n");
        }

        /// 写
        private void WriteBtn_Click(object sender, RoutedEventArgs e)
        {
            //把D8100的16个位全写1
            //omron.WriteDBits(8100, 0, 16, new bool[] { true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true });
            //把D8100的第一个位写0
            //omron.WriteDBits(8100, 0, 1, new bool[] {false });
            //把D8100写0
            //omron.WriteDWords(8100, 1, new int[] { 0 });
            //存入到数据库
            //string dayAddress = "date,H0809,H0910,H1011,H1112,H1213,H1314,H1415,H1516,H1617,H1718,H1819,H1920,allh";
            //string nightaddress = "date,h2021,h2122,h2223,h2324,h2401,h0102,h0203,h0304,h0405,h0506,h0607,h0708,allh";
            //string[] JsonData = { "2021-09-14", "5555", "3343", "1011", "233", "333", "1314", "1415", "1516", "1617", "1718", "1819", "1920", "666" };
            //string strSql = "insert into dayyeild(" + dayAddress + ") values('" + JsonData[0] + "','" + JsonData[1] + "','" + JsonData[2] + "','" + JsonData[3] + "','" + JsonData[4] + "','" + JsonData[5] + "','" + JsonData[6] + "','" + JsonData[7] + "','" + JsonData[8] + "','" + JsonData[9] + "','" + JsonData[10] + "','" + JsonData[11] + "','" + JsonData[12] + "','" + JsonData[13] + "')";
            //DbHelperSQL.ExecuteSql(strSql);
        }

        //写位
        private void WriteBits()
        {
            //把D8100的16个位全写1
            ////omron.writedbits(8100, 0, a16, new bool[] { true, true,true, true, true, true, true, true, true, true, true, true, true, true, true, true });
            //把D8100的第一个位写0
            //omron.WriteDBits(8100, 0, 1, new bool[] {false });
            //把D8100写0
            //omron.WriteDWords(8100, 1, new int[] { 0 });
            //存入到数据库

        }



        //-------------------------------绿点数据采集程序块-------------------------------------------------------------------------------------



        //读字-产量采集--1
        string[] DBtable = { "dayyeild", "nightyeild" };
        string dayAddress = "date,H0809,H0910,H1011,H1112,H1213,H1314,H1415,H1516,H1617,H1718,H1819,H1920,dclass,type";
        string clockRunLog = "";
        private void ReadBtn_Click(object sender, RoutedEventArgs e)
        {
            ArrayList AllYeild = new ArrayList();
            try
            {
                string[] res;
                string str1 = "4440";//Madress.Text;
                string conutNB = "24";// conut.Text;
                if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(conutNB))
                {
                    MessageBox.Show("地址和不能数量为空！", "产量内存连接失败");
                    WriteLogs("log.txt", "RunLog", "产量内存连接失败");

                }
                else
                {
                    omron.ReadDWords(int.Parse(str1), int.Parse(conutNB), out res);
                    DateTime dt1 = DateTime.Now;
                    while ((DateTime.Now - dt1).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    // 输出d地址
                    //ValueBox.AppendText("读字:" + int.Parse(str1) + "=> ");
                    clockRunLog = "读字:" + int.Parse(str1) + "=> ";
                    int i = 0;
                    int n = 0;
                    int m = 0;
                    var nStr = "";
                    var mStr = "";
                    Console.WriteLine("产量res:" + res + "res.length:", res.Length);
                    if (res.Length < 1)
                    {
                        Console.WriteLine("产量res数据不对:", res);
                        //ValueBox.AppendText("地址和不能数量为空！" + "连接失败\n");
                        WriteLogs("log.txt", "RunLog", "重新调用ReadBtn_Click");
                        return;
                    }
                    else
                    {
                        foreach (string item in res)
                        {
                            //ValueBox.AppendText(item);
                            //ValueBox.AppendText(" ");//添加空格
                            var hex2To10 = Convert.ToInt32(res[i], 16);
                            if (n == 0)
                            {
                                nStr = item;
                                n++;
                            }
                            else
                            {
                                mStr = nStr + item;
                                n = 0;
                                //Console.WriteLine("Mstr:" + mStr);
                                var hexTo10 = Convert.ToInt32(mStr, 16);
                                AllYeild.Add(hexTo10);
                                //Console.WriteLine("hexTo10:" + hexTo10);
                                m++;
                            }
                            //System.Diagnostics.Debug.WriteLine("item+res:" + item + "-" + res[i]);
                            i++;
                        }
                        //ValueBox.AppendText("\n");
                        //System.Diagnostics.Debug.WriteLine("Allyeild产量:" + AllYeild[0]);
                        Console.WriteLine("AllYeild.length:" + AllYeild.Count); //使用数量
                        string type = "产量(pcs)";
                        DateTime dt11 = DateTime.Now;
                        while ((DateTime.Now - dt11).TotalMilliseconds < 50)
                        {
                            continue;
                        };
                        InsertDB(DBtable, dayAddress, AllYeild, type);
                    }
                }
            }
            catch (Exception ex)
            {
                WriteLogs("log.txt", "产量异常:", ex.Message.ToString());
            }

            return;
        }


        //数据库操作-产量采集操作---1
        private void InsertDB(string[] DBtable, string dayAddress, ArrayList AllYeild, string type)
        {
            try
            {
                string ldclass;
                DateTime dt = DateTime.Now;
                string dt24 = dt.ToString("HH");
                string DT24 = dt.ToString("yyyyMMdd");
                //DateTime.Parse(DT24).ToString("yyyyMMdd");
                System.Diagnostics.Debug.WriteLine("时间:" + dt24 + "/" + int.Parse(dt24) + "/" + DT24);
                string today = dt.ToString("yyyyMMdd");
                if (int.Parse(dt24) < 8 || int.Parse(dt24) >= 20) //判断为晚班
                {
                    //晚班产量数据
                    System.Diagnostics.Debug.WriteLine("班别：晚班" + dt24);
                    ldclass = "晚班";
                    if (int.Parse(dt24) < 8 && int.Parse(dt24) >= 0) //晚班时间为0-8之间减1判断
                    {
                        System.Diagnostics.Debug.WriteLine("日期DT24：" + DT24);
                        DT24 = (int.Parse(DT24) - 1).ToString();
                        System.Diagnostics.Debug.WriteLine("日期DT24-1：" + DT24);
                    }
                    else
                    {      //时间为20-24之间
                        System.Diagnostics.Debug.WriteLine("dt24" + dt24 + "DT24:" + DT24);
                    }
                    //查询是否已经存在记录
                    string SqlStatus = (DbHelperSQL.GetNightMaxID("id", "dayyeild", DT24, type)).ToString();
                    DateTime dt1 = DateTime.Now;
                    while ((DateTime.Now - dt1).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    Console.WriteLine("SqlStatus0:" + SqlStatus);
                    string strSql;
                    if (string.IsNullOrEmpty(SqlStatus))
                    {
                        StatusLight1.Fill = omron.IsConnected ? new SolidColorBrush(Colors.LimeGreen) : new SolidColorBrush(Colors.Red);
                    }
                    if (int.Parse(SqlStatus) <= 1)
                    {
                        //晚班产量数据新增1条
                        Console.WriteLine("查询晚班数据条不存在" + SqlStatus);
                        strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[20] + "','" + AllYeild[21] + "','" + AllYeild[22] + "','" + AllYeild[23] + "','" + AllYeild[0] + "','" + AllYeild[1] + "','" + AllYeild[2] + "','" + AllYeild[3] + "','" + AllYeild[4] + "','" + AllYeild[5] + "','" + AllYeild[6] + "','" + AllYeild[7] + "','" + ldclass + "','" + type + "')";   //+ "9999" +"','"                                                                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                                                             //Console.WriteLine("strSql0:" + strSql);                
                    }
                    else
                    {
                        //晚班产量数据更新1条
                        Console.WriteLine("查询数据条已经存在" + SqlStatus);
                        strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[20] + ",H0910=" + AllYeild[21] + ",H1011=" + AllYeild[22] + ",H1112=" + AllYeild[23] + ",H1213=" + AllYeild[0] + ",H1314=" + AllYeild[1] + ",H1415=" + AllYeild[2] + ",H1516=" + AllYeild[3] + ",H1617=" + AllYeild[4] + ",H1718=" + AllYeild[5] + ",H1819=" + AllYeild[6] + ",H1920=" + AllYeild[7] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus;
                        //Console.WriteLine("strSql1:" + strSql);

                    }

                    //Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));
                    int sqlok = DbHelperSQL.ExecuteSql(strSql);//数据库保存
                    DateTime dt12 = DateTime.Now;
                    while ((DateTime.Now - dt12).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    if (sqlok == 1 && run == true)
                    {
                        Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + sqlok);
                       
                    }
                    else
                    {
                        MessageBox.Show("数据库操作错误：", "数据库查询失败！");
                        WriteLogs("log.txt", "RunLog", "产量数据库操作失败");
                        
                    }
                }
                else  //白班
                {
                    ldclass = "白班";
                    System.Diagnostics.Debug.WriteLine("班别：" + ldclass + "时间：" + dt24);
                    //查询白班是否已经存在当天的记录
                    string SqlStatus1 = (DbHelperSQL.GetDayMaxID("id", "dayyeild", today, type)).ToString();
                    DateTime dt13 = DateTime.Now;
                    while ((DateTime.Now - dt13).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    Console.WriteLine("SqlStatus1:" + SqlStatus1);
                    string strSql;//白班数据库操作膜版
                    if (int.Parse(SqlStatus1) <= 1)
                    {
                        //白班产量数据新增1条
                        Console.WriteLine("查询数据条不存在" + SqlStatus1);
                        strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[8] + "','" + AllYeild[9] + "','" + AllYeild[10] + "','" + AllYeild[11] + "','" + AllYeild[12] + "','" + AllYeild[13] + "','" + AllYeild[14] + "','" + AllYeild[5] + "','" + AllYeild[16] + "','" + AllYeild[17] + "','" + AllYeild[18] + "','" + AllYeild[19] + "','" + ldclass + "','" + type + "')";
                        Console.WriteLine("strSql1:" + strSql);

                    }
                    else
                    {
                        //白班产量数据更新1条
                        Console.WriteLine("查询数据条已经存在" + SqlStatus1);
                        strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[8] + ",H0910=" + AllYeild[9] + ",H1011=" + AllYeild[10] + ",H1112=" + AllYeild[11] + ",H1213=" + AllYeild[12] + ",H1314=" + AllYeild[13] + ",H1415=" + AllYeild[14] + ",H1516=" + AllYeild[5] + ",H1617=" + AllYeild[16] + ",H1718=" + AllYeild[17] + ",H1819=" + AllYeild[18] + ",H1920=" + AllYeild[19] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus1;
                        Console.WriteLine("strSql1:" + strSql);
                    }
                    int sqlok = DbHelperSQL.ExecuteSql(strSql);
                    DateTime dt1 = DateTime.Now;
                    while ((DateTime.Now - dt1).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    if (sqlok == 1 && run == true)
                    {
                        Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + sqlok);
                    }
                    else
                    {
                        MessageBox.Show("数据库操作错误：", "产量数据库操作失败！");
                        WriteLogs("log.txt", "RunLog", "产量数据库操作失败");
                    }
                }
                AllYeild.Clear(); //清除数组列表
            }
            catch (Exception ex)
            {
                WriteLogs("log.txt", "Exception:", ex.Message.ToString());

            }
            return;
        }


        //读字-待料时间采集---2
        private void ReadWaitTime2(object sender, RoutedEventArgs e)
        {
            ArrayList AllYeild = new ArrayList();
            try
            {
                string[] res;
                string str1 = "4500";// writTime.Text;
                string conutNB = "48";// writTime_count.Text;
                if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(conutNB))
                {
                    MessageBox.Show("地址和不能数量为空！", "待料内存连接失败");
                    WriteLogs("log.txt", "RunLog", "待料内存连接失败");

                }
                else
                {
                    omron.ReadDWords(int.Parse(str1), int.Parse(conutNB), out res);
                    DateTime dt13 = DateTime.Now;
                    while ((DateTime.Now - dt13).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    // 输出d地址
                    //ValueBox.AppendText("读字:" + int.Parse(str1) + "=> ");
                    int i = 0;
                    int n = 0;
                    int m = 0;
                    var nStr = "";
                    var mStr = "";
                    Console.WriteLine("待料时间res:", res, "res.length:", res.Length);
                    if (res.Length < 1)
                    {
                        Console.WriteLine("待料res数据不对:", res);
                        WriteLogs("log.txt", "RunLog", "待料res数据不对");
                        return;
                    }
                    else
                    {
                        foreach (string item in res)
                        {
                            //ValueBox.AppendText(item);
                            //ValueBox.AppendText(" ");//添加空格
                            var hex2To10 = Convert.ToInt32(res[i], 16);
                            if (n == 0)
                            {
                                nStr = item;
                                n++;
                            }
                            else
                            {
                                mStr = nStr + item;
                                n = 0;
                                //Console.WriteLine("Mstr:" + mStr);
                                var hexTo10 = Convert.ToInt32(mStr, 16);
                                AllYeild.Add(hexTo10);
                                //Console.WriteLine("hexTo10:" + hexTo10);
                                m++;
                            }
                            //System.Diagnostics.Debug.WriteLine("item+res:" + item + "-" + res[i]);
                            i++;
                        }
                        //System.Diagnostics.Debug.WriteLine("Allyeild待料:" + AllYeild[0]);
                        //ValueBox.AppendText("\n");
                        Console.WriteLine("AllYeild.length:" + AllYeild.Count); //使用数量
                        string type = "待料时间(分钟)";
                        DateTime dt1 = DateTime.Now;
                        while ((DateTime.Now - dt1).TotalMilliseconds < 50)
                        {
                            continue;
                        };
                        InsertDB2(DBtable, dayAddress, AllYeild, type);

                    }
                }
                //Console.WriteLine("AllYeild.length:" + AllYeild.Capacity);//容器容量
            }
            catch (Exception ex)
            {
                WriteLogs("log.txt", "Exception:", ex.Message.ToString());
            }
            return;
        }


        //数据库操作-待料时间储存----2
        private void InsertDB2(string[] DBtable, string dayAddress, ArrayList AllYeild, string type)
        {
            try
            {
                string ldclass;
                DateTime dt = DateTime.Now;
                string dt24 = dt.ToString("HH");
                string DT24 = dt.ToString("yyyyMMdd");
                //DateTime.Parse(DT24).ToString("yyyyMMdd");
                System.Diagnostics.Debug.WriteLine("时间:" + dt24 + "/" + int.Parse(dt24) + "/" + DT24);
                string today = dt.ToString("yyyyMMdd");
                if (int.Parse(dt24) < 8 || int.Parse(dt24) >= 20)
                {
                    //晚班待料数据
                    System.Diagnostics.Debug.WriteLine("班别：晚班" + dt24);
                    ldclass = "晚班";
                    if (int.Parse(dt24) < 8 && int.Parse(dt24) >= 0) //晚班时间减1判断
                    {
                        System.Diagnostics.Debug.WriteLine("日期DT24：" + DT24);
                        DT24 = (int.Parse(DT24) - 1).ToString();
                        System.Diagnostics.Debug.WriteLine("日期DT24-1：" + DT24);
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("dt24" + dt24 + "DT24:" + DT24);
                    }

                    string SqlStatus = (DbHelperSQL.GetNightMaxID("id", "dayyeild", DT24, type)).ToString();
                    DateTime dt13 = DateTime.Now;
                    while ((DateTime.Now - dt13).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    Console.WriteLine("SqlStatus0:" + SqlStatus);
                    string strSql;
                    if (int.Parse(SqlStatus) <= 1)
                    {
                        //晚班待料数据新增1条
                        Console.WriteLine("查询晚班数据条不存在" + SqlStatus);
                        strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[40] + "','" + AllYeild[42] + "','" + AllYeild[44] + "','" + AllYeild[46] + "','" + AllYeild[0] + "','" + AllYeild[2] + "','" + AllYeild[4] + "','" + AllYeild[6] + "','" + AllYeild[8] + "','" + AllYeild[10] + "','" + AllYeild[12] + "','" + AllYeild[14] + "','" + ldclass + "','" + type + "')";   //+ "9999" +"','" 
                        Console.WriteLine("strSql0:" + strSql);
                    }
                    else
                    {
                        //晚班待料数据更新1条
                        Console.WriteLine("查询数据条已经存在" + SqlStatus);
                        strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[40] + ",H0910=" + AllYeild[42] + ",H1011=" + AllYeild[44] + ",H1112=" + AllYeild[46] + ",H1213=" + AllYeild[0] + ",H1314=" + AllYeild[2] + ",H1415=" + AllYeild[4] + ",H1516=" + AllYeild[6] + ",H1617=" + AllYeild[8] + ",H1718=" + AllYeild[10] + ",H1819=" + AllYeild[12] + ",H1920=" + AllYeild[14] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus;
                        Console.WriteLine("strSql1:" + strSql);
                    }
                    //Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));
                    int sqlok = DbHelperSQL.ExecuteSql(strSql);
                    DateTime dt23 = DateTime.Now;
                    while ((DateTime.Now - dt23).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    if (sqlok == 1 && run == true)
                    {
                        Console.WriteLine("DbHelperSQL待料数据操作1为成功,操作返回为:" + sqlok);
                       
                    }
                    else
                    {
                        WriteLogs("log.txt", "RunLog", "待料数据库操作失败");
                    }

                }
                else
                {
                    ldclass = "白班";
                    System.Diagnostics.Debug.WriteLine("班别：" + ldclass);
                    string SqlStatus1 = (DbHelperSQL.GetDayMaxID("id", "dayyeild", today, type)).ToString();
                    DateTime dt23 = DateTime.Now;
                    while ((DateTime.Now - dt23).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    Console.WriteLine("SqlStatus1:" + SqlStatus1);
                    string strSql;//白班数据库操作膜版
                    if (int.Parse(SqlStatus1) <= 1)
                    {
                        //白班待料数据新增1条
                        Console.WriteLine("查询数据条不存在" + SqlStatus1);
                        strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[16] + "','" + AllYeild[18] + "','" + AllYeild[20] + "','" + AllYeild[22] + "','" + AllYeild[24] + "','" + AllYeild[26] + "','" + AllYeild[28] + "','" + AllYeild[30] + "','" + AllYeild[32] + "','" + AllYeild[34] + "','" + AllYeild[36] + "','" + AllYeild[38] + "','" + ldclass + "','" + type + "')";
                        Console.WriteLine("strSql1:" + strSql);

                    }
                    else
                    {
                        //白班待料数据更新1条
                        Console.WriteLine("查询数据条已经存在" + SqlStatus1);
                        strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[16] + ",H0910=" + AllYeild[18] + ",H1011=" + AllYeild[20] + ",H1112=" + AllYeild[22] + ",H1213=" + AllYeild[24] + ",H1314=" + AllYeild[26] + ",H1415=" + AllYeild[28] + ",H1516=" + AllYeild[30] + ",H1617=" + AllYeild[32] + ",H1718=" + AllYeild[34] + ",H1819=" + AllYeild[36] + ",H1920=" + AllYeild[38] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus1;
                        Console.WriteLine("strSql1:" + strSql);
                    }
                    //Console.WriteLine("DbHelperSQL数据操作1,1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));
                    int sqlok = DbHelperSQL.ExecuteSql(strSql);
                    DateTime dt25 = DateTime.Now;
                    while ((DateTime.Now - dt25).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    if (sqlok == 1 && run == true)
                    {
                        Console.WriteLine("DbHelperSQL白班待料数据操作1为成功,操作返回为:" + sqlok);
                       
                    }
                    else
                    {
                        WriteLogs("log.txt", "RunLog", "白班待料数据库操作失败");

                    }
                }
                AllYeild.Clear(); //清除数组列表
            }
            catch (Exception e)
            {
                WriteLogs("log.txt", "Exception:", e.Message.ToString());
            }
            return;
        }


        //读字-故障时间集---3
        private void ReadBugTime3(object sender, RoutedEventArgs e)
        {
            ArrayList AllYeild = new ArrayList();
            try
            {
                string[] res;
                string str1 = "4550";// bugTime.Text;
                string conutNB = "48";// bugTime_count.Text;
                if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(conutNB))
                {
                    MessageBox.Show("地址和不能数量为空！", "读取失败");
                    WriteLogs("log.txt", "RunLog", "读取连接失败");

                }
                else
                {
                    omron.ReadDWords(int.Parse(str1), int.Parse(conutNB), out res);
                    DateTime dt31 = DateTime.Now;
                    while ((DateTime.Now - dt31).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    // 输出d地址
                    //ValueBox.AppendText("读字:" + int.Parse(str1) + "=> ");

                    int i = 0;
                    int n = 0;
                    int m = 0;
                    var nStr = "";
                    var mStr = "";
                    Console.WriteLine("故障时间res:" + res + "res.length:" + res.Length);
                    if (res.Length < 1)
                    {
                        Console.WriteLine("故障时间res数据不对:", res);
                        WriteLogs("log.txt", "待料", "待料res数据不对");
                        return;
                    }
                    else
                    {
                        foreach (string item in res)
                        {
                            //ValueBox.AppendText(item);
                            //ValueBox.AppendText(" ");//添加空格
                            var hex2To10 = Convert.ToInt32(res[i], 16);
                            if (n == 0)
                            {
                                nStr = item;
                                n++;
                            }
                            else
                            {
                                mStr = nStr + item;
                                n = 0;
                                //Console.WriteLine("Mstr:" + mStr);
                                var hexTo10 = Convert.ToInt32(mStr, 16);
                                AllYeild.Add(hexTo10);
                                //Console.WriteLine("hexTo10:" + hexTo10);
                                m++;
                            }

                            i++;
                        }
                    }
                    //System.Diagnostics.Debug.WriteLine("Allyeild故障时间:" + AllYeild[0]);
                    Console.WriteLine("AllYeild.length:" + AllYeild.Count); //使用数量                                                        //Console.WriteLine("AllYeild.length:" + AllYeild.Capacity);//容器容量
                    //ValueBox.AppendText("\n");
                    //报存数据
                   
                    string type = "故障时间(分钟)";
                    DateTime dt1 = DateTime.Now;
                    while ((DateTime.Now - dt1).TotalMilliseconds < 50)
                    {
                        continue;
                    };
                    InsertDB3(DBtable, dayAddress, AllYeild, type);
                }
            }
            catch (Exception ex)
            {
                WriteLogs("log.txt", "Exception:", ex.Message.ToString());
            }
            return;
        }


        //数据库操作-故障时间储存----3
        private void InsertDB3(string[] DBtable, string dayAddress, ArrayList AllYeild, string type)
        {
            try
            {
                string ldclass;
                DateTime dt = DateTime.Now;
                string dt24 = dt.ToString("HH");
                string DT24 = dt.ToString("yyyyMMdd");
                //DateTime.Parse(DT24).ToString("yyyyMMdd");
                System.Diagnostics.Debug.WriteLine("时间:" + dt24 + "/" + int.Parse(dt24) + "/" + DT24);
                string today = dt.ToString("yyyyMMdd");
                if (int.Parse(dt24) < 8 || int.Parse(dt24) >= 20)
                {
                    //晚班故障数据日期判断修正
                    System.Diagnostics.Debug.WriteLine("班别：晚班" + dt24);
                    ldclass = "晚班";
                    if (int.Parse(dt24) < 8 && int.Parse(dt24) >= 0) //晚班时间减1判断
                    {
                        System.Diagnostics.Debug.WriteLine("日期DT24：" + DT24);
                        DT24 = (int.Parse(DT24) - 1).ToString();
                        System.Diagnostics.Debug.WriteLine("日期DT24-1：" + DT24);
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("dt24" + dt24 + "DT24:" + DT24);
                    }
                   
                    //查询当前晚班数据是否存在
                    string SqlStatus = (DbHelperSQL.GetNightMaxID("id", "dayyeild", DT24, type)).ToString();//DT24是根据20-24点处理后的时间
                    Console.WriteLine("SqlStatus0:" + SqlStatus);
                    DateTime dt13 = DateTime.Now;
                    while ((DateTime.Now - dt13).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    string strSql;
                    if (int.Parse(SqlStatus) <= 1)
                    {
                        //晚班故障数据新增1条
                        Console.WriteLine("查询晚班数据条不存在" + SqlStatus);
                        strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[40] + "','" + AllYeild[42] + "','" + AllYeild[44] + "','" + AllYeild[46] + "','" + AllYeild[0] + "','" + AllYeild[2] + "','" + AllYeild[4] + "','" + AllYeild[6] + "','" + AllYeild[8] + "','" + AllYeild[10] + "','" + AllYeild[12] + "','" + AllYeild[14] + "','" + ldclass + "','" + type + "')";   //+ "9999" +"','" 
                        Console.WriteLine("strSql0:" + strSql);
                    }
                    else
                    {
                        //晚班故障数据更新1条
                        Console.WriteLine("查询数据条已经存在" + SqlStatus);
                        strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[40] + ",H0910=" + AllYeild[42] + ",H1011=" + AllYeild[44] + ",H1112=" + AllYeild[46] + ",H1213=" + AllYeild[0] + ",H1314=" + AllYeild[2] + ",H1415=" + AllYeild[4] + ",H1516=" + AllYeild[6] + ",H1617=" + AllYeild[8] + ",H1718=" + AllYeild[10] + ",H1819=" + AllYeild[12] + ",H1920=" + AllYeild[14] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus;
                        Console.WriteLine("strSql1:" + strSql);
                    }


                    //Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));

                    int sqlok = DbHelperSQL.ExecuteSql(strSql);  //保存数据
                    DateTime dt43 = DateTime.Now;
                    while ((DateTime.Now - dt43).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    if (sqlok == 1 && run == true)
                    {
                        Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + sqlok);
                    }
                    else
                    {
                        WriteLogs("log.txt", "RunLog", "晚班故障时间数据库操作失败");
                    }
                }
                else
                {
                    ldclass = "白班";
                    System.Diagnostics.Debug.WriteLine("班别：" + ldclass);
                    string SqlStatus1 = (DbHelperSQL.GetDayMaxID("id", "dayyeild", today, type)).ToString(); //today是今天日期
                    DateTime dt33 = DateTime.Now;
                    while ((DateTime.Now - dt33).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    Console.WriteLine("SqlStatus1:" + SqlStatus1);
                    string strSql;//白班数据库操作膜版
                    if (int.Parse(SqlStatus1) <= 1)
                    {
                        //白班故障数据新增1条
                        Console.WriteLine("查询数据条不存在" + SqlStatus1);
                        strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[16] + "','" + AllYeild[18] + "','" + AllYeild[20] + "','" + AllYeild[22] + "','" + AllYeild[24] + "','" + AllYeild[26] + "','" + AllYeild[28] + "','" + AllYeild[30] + "','" + AllYeild[32] + "','" + AllYeild[34] + "','" + AllYeild[36] + "','" + AllYeild[38] + "','" + ldclass + "','" + type + "')";
                        Console.WriteLine("strSql1:" + strSql);
                    }
                    else
                    {
                        //白班故障数据更新1条
                        Console.WriteLine("查询数据条已经存在" + SqlStatus1);
                        strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[16] + ",H0910=" + AllYeild[18] + ",H1011=" + AllYeild[20] + ",H1112=" + AllYeild[22] + ",H1213=" + AllYeild[24] + ",H1314=" + AllYeild[26] + ",H1415=" + AllYeild[28] + ",H1516=" + AllYeild[30] + ",H1617=" + AllYeild[32] + ",H1718=" + AllYeild[34] + ",H1819=" + AllYeild[36] + ",H1920=" + AllYeild[38] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus1;
                        Console.WriteLine("strSql1:" + strSql);
                    }
                    //Console.WriteLine("DbHelperSQL数据操作3,1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));

                    int sqlok = DbHelperSQL.ExecuteSql(strSql);
                    DateTime dt13 = DateTime.Now;
                    while ((DateTime.Now - dt13).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    if (sqlok == 1 && run == true)
                    {
                        Console.WriteLine("DbHelperSQL故障时间白班数据库操作1为成功,操作返回为:" + sqlok);
                    }
                    else
                    {
                        WriteLogs("log.txt", "RunLog", "白班故障时间数据库操作失败");
                    }
                }
                AllYeild.Clear(); //清除数组列表
            }
            catch (Exception e)
            {
                WriteLogs("log.txt", "RunLog", e.Message);
                throw e;
            }
            return;
        }


        //读字-左抛膜次数采集---4
        private void ReadZuopao4(object sender, RoutedEventArgs e)
        {
            ArrayList AllYeild = new ArrayList();
            try
            {
                string[] res;
                string str1 = "4700";// zuopao.Text;
                string conutNB = "48";// zuopao_count.Text;
                if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(conutNB))
                {
                    MessageBox.Show("地址和不能数量为空！", "连接失败");
                }
                else
                {
                    omron.ReadDWords(int.Parse(str1), int.Parse(conutNB), out res);
                    DateTime dt13 = DateTime.Now;
                    while ((DateTime.Now - dt13).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    // 输出d地址
                    //ValueBox.AppendText("读字:" + int.Parse(str1) + "=> ");
                    int i = 0;
                    int n = 0;
                    int m = 0;
                    var nStr = "";
                    var mStr = "";
                    Console.WriteLine("左边res:" + res + "res.length:" + res.Length);
                    if (res.Length < 1)
                    {
                        Console.WriteLine("左边res数据不对:", res);
                        WriteLogs("log.txt", "待料", "待料res数据不对");
                        return;
                    }
                    else
                    {
                        foreach (string item in res)
                        {
                            //ValueBox.AppendText(item);
                            //ValueBox.AppendText(" ");//添加空格
                            var hex2To10 = Convert.ToInt32(res[i], 16);
                            if (n == 0)
                            {
                                nStr = item;
                                n++;
                            }
                            else
                            {
                                mStr = nStr + item;
                                n = 0;
                                //Console.WriteLine("Mstr:" + mStr);
                                var hexTo10 = Convert.ToInt32(mStr, 16);
                                AllYeild.Add(hexTo10);
                                //Console.WriteLine("hexTo10:" + hexTo10);
                                m++;
                            }
                            //System.Diagnostics.Debug.WriteLine("item+res:" + item + "-" + res[i]);
                            i++;
                        }
                    }
                    //System.Diagnostics.Debug.WriteLine("Allyeild左抛次数:" + AllYeild[0]);
                    Console.WriteLine("AllYeild.length:" + AllYeild.Count); //使用数量
                    //ValueBox.AppendText("\n");
                  
                    //报存数据
                    string type = "左抛膜(pcs)";
                    WriteLogs("log.txt", "RunLog", "InsertDB4");
                    DateTime dt1 = DateTime.Now;
                    while ((DateTime.Now - dt1).TotalMilliseconds < 50)
                    {
                        continue;
                    };
                    InsertDB4(DBtable, dayAddress, AllYeild, type);
                }

            }
            catch (Exception ex)
            {
                WriteLogs("log.txt", "Exception:", ex.Message.ToString());
            }
            return;
        }


        //数据库操作-左抛膜次数储存----4
        private void InsertDB4(string[] DBtable, string dayAddress, ArrayList AllYeild, string type)
        {
            try
            {
                string ldclass;
                DateTime dt = DateTime.Now;
                string dt24 = dt.ToString("HH");
                string DT24 = dt.ToString("yyyyMMdd");
                //DateTime.Parse(DT24).ToString("yyyyMMdd");
                System.Diagnostics.Debug.WriteLine("时间:" + dt24 + "/" + int.Parse(dt24) + "/" + DT24);
                string today = dt.ToString("yyyyMMdd");
                if (int.Parse(dt24) < 8 || int.Parse(dt24) >= 20)
                {
                    //晚班左边抛膜数据日期修正
                    System.Diagnostics.Debug.WriteLine("班别：晚班" + dt24);
                    ldclass = "晚班";

                    if (int.Parse(dt24) < 8 && int.Parse(dt24) >= 0) //晚班时间减1判断
                    {
                        System.Diagnostics.Debug.WriteLine("日期DT24：" + DT24);
                        DT24 = (int.Parse(DT24) - 1).ToString();
                        System.Diagnostics.Debug.WriteLine("日期DT24-1：" + DT24);
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("dt24" + dt24 + "DT24:" + DT24);
                    }

                    //晚班左抛摸查询是否存在
                    string SqlStatus = (DbHelperSQL.GetNightMaxID("id", "dayyeild", DT24, type)).ToString();//查现场是否存在
                    DateTime dt13 = DateTime.Now;
                    while ((DateTime.Now - dt13).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    Console.WriteLine("SqlStatus0:" + SqlStatus);
                    string strSql;
                    if (int.Parse(SqlStatus) <= 1)
                    {
                        //晚班左边数据新增1条
                        Console.WriteLine("查询晚班数据条不存在" + SqlStatus);
                        strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[40] + "','" + AllYeild[42] + "','" + AllYeild[44] + "','" + AllYeild[46] + "','" + AllYeild[0] + "','" + AllYeild[2] + "','" + AllYeild[4] + "','" + AllYeild[6] + "','" + AllYeild[8] + "','" + AllYeild[10] + "','" + AllYeild[12] + "','" + AllYeild[14] + "','" + ldclass + "','" + type + "')";   //+ "9999" +"','" 
                        Console.WriteLine("strSql0:" + strSql);
                    }
                    else
                    {
                        //晚班左边抛膜数据更新1条
                        Console.WriteLine("查询数据条已经存在" + SqlStatus);
                        strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[40] + ",H0910=" + AllYeild[42] + ",H1011=" + AllYeild[44] + ",H1112=" + AllYeild[46] + ",H1213=" + AllYeild[0] + ",H1314=" + AllYeild[2] + ",H1415=" + AllYeild[4] + ",H1516=" + AllYeild[6] + ",H1617=" + AllYeild[8] + ",H1718=" + AllYeild[10] + ",H1819=" + AllYeild[12] + ",H1920=" + AllYeild[14] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus;
                        Console.WriteLine("strSql1:" + strSql);
                    }

                    //
                    //Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));
                    int sqlok = DbHelperSQL.ExecuteSql(strSql);
                    DateTime dt43 = DateTime.Now;
                    while ((DateTime.Now - dt43).TotalMilliseconds < 100)
                    {
                        continue;
                    };
                    if (sqlok == 1 && run == true)
                    {
                        Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + sqlok);
                       
                    }
                    else
                    {
                        WriteLogs("log.txt", "RunLog", "左抛摸数据库操作失败");
                    }
                }
                else
                {
                    ldclass = "白班";
                    System.Diagnostics.Debug.WriteLine("班别：" + ldclass + dt24);
                    string SqlStatus1 = (DbHelperSQL.GetDayMaxID("id", "dayyeild", today, type)).ToString();//查询
                    DateTime dt44 = DateTime.Now;
                    while ((DateTime.Now - dt44).TotalMilliseconds < 100)
                    {
                        continue;
                    };

                    Console.WriteLine("SqlStatus1:" + SqlStatus1);
                    string strSql;//白班数据库操作膜版
                    if (int.Parse(SqlStatus1) <= 1)
                    {
                        //白班左边抛膜数据新增1条
                        Console.WriteLine("查询数据条不存在" + SqlStatus1);
                        strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[16] + "','" + AllYeild[18] + "','" + AllYeild[20] + "','" + AllYeild[22] + "','" + AllYeild[24] + "','" + AllYeild[26] + "','" + AllYeild[28] + "','" + AllYeild[30] + "','" + AllYeild[32] + "','" + AllYeild[34] + "','" + AllYeild[36] + "','" + AllYeild[38] + "','" + ldclass + "','" + type + "')";
                        Console.WriteLine("strSql1:" + strSql);

                    }
                    else
                    {
                        //白班左边抛膜数据更新1条
                        Console.WriteLine("查询数据条已经存在" + SqlStatus1);
                        strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[16] + ",H0910=" + AllYeild[18] + ",H1011=" + AllYeild[20] + ",H1112=" + AllYeild[22] + ",H1213=" + AllYeild[24] + ",H1314=" + AllYeild[26] + ",H1415=" + AllYeild[28] + ",H1516=" + AllYeild[30] + ",H1617=" + AllYeild[32] + ",H1718=" + AllYeild[34] + ",H1819=" + AllYeild[36] + ",H1920=" + AllYeild[38] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus1;
                        Console.WriteLine("strSql1:" + strSql);
                    }
                    //Console.WriteLine("DbHelperSQL数据操作3,1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));
                    DateTime dt45 = DateTime.Now;
                    while ((DateTime.Now - dt45).TotalMilliseconds < 100)
                    {
                        continue;
                    };

                    int sqlok = DbHelperSQL.ExecuteSql(strSql);
                    if (sqlok == 1 && run == true)
                    {
                        Console.WriteLine("DbHelperSQL白班左抛摸数据操作1为成功,操作返回为:" + sqlok);
                   
                    }
                    else {
                        WriteLogs("log.txt", "RunLog", "白班左抛摸数据库操作失败");
                    }
                }
                AllYeild.Clear(); //清除数组列表
            }
            catch (Exception e)
            {
                WriteLogs("log.txt", "Exception:", e.Message.ToString());
            }
            return;
        }


        //读字-右抛膜次数采集---5
        private void ReadYoupao5(object sender, RoutedEventArgs e)
        {
            ArrayList AllYeild = new ArrayList();
            try
            {
                string[] res;
                string str1 = "4800";//youpao.Text;
                string conutNB = "48";// youpao_count.Text;
                if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(conutNB))
                {
                    MessageBox.Show("地址和不能数量为空！", "连接失败");
                    WriteLogs("log.txt", "RunLog", "内存地址连接失败");

                }
                else
                {
                    omron.ReadDWords(int.Parse(str1), int.Parse(conutNB), out res);
                    DateTime dt51 = DateTime.Now;
                    while ((DateTime.Now - dt51).TotalMilliseconds < 100)
                    {
                        continue;
                    };

                    // 输出d地址
                    //ValueBox.AppendText("读字:" + int.Parse(str1) + "=> ");
                    int i = 0;
                    int n = 0;
                    int m = 0;
                    var nStr = "";
                    var mStr = "";
                    Console.WriteLine("右边抛膜res:" + res + "res.length:" + res.Length);
                    if (res.Length < 1)
                    {
                        Console.WriteLine("右边抛膜res数据不对:", res);
                        WriteLogs("log.txt", "待料", "待料res数据不对");
                        return;
                    }
                    else
                    {
                        foreach (string item in res)
                        {
                            //ValueBox.AppendText(item);
                            //ValueBox.AppendText(" ");//添加空格
                            var hex2To10 = Convert.ToInt32(res[i], 16);
                            if (n == 0)
                            {
                                nStr = item;
                                n++;
                            }
                            else
                            {
                                mStr = nStr + item;
                                n = 0;
                                //Console.WriteLine("Mstr:" + mStr);
                                var hexTo10 = Convert.ToInt32(mStr, 16);
                                AllYeild.Add(hexTo10);
                                //Console.WriteLine("hexTo10:" + hexTo10);
                                m++;
                            }
                            //System.Diagnostics.Debug.WriteLine("item+res:" + item + "-" + res[i]);
                            i++;
                        }
                    }
                    //System.Diagnostics.Debug.WriteLine("Allyeild右抛次数:" + AllYeild[0]);
                    Console.WriteLine("AllYeild.length:" + AllYeild.Count); //使用数量
                    //报存数据
                    string type = "右抛膜(pcs)";
                    DateTime dt1 = DateTime.Now;
                    while ((DateTime.Now - dt1).TotalMilliseconds < 50)
                    {
                        continue;
                    };
                    InsertDB5(DBtable, dayAddress, AllYeild, type);
                }

            }
            catch (Exception ex)
            {
                WriteLogs("log.txt", "Exception:", ex.Message.ToString());
            }
            return;
        }


        //数据库操作-右抛膜次数储存----5
        private void InsertDB5(string[] DBtable, string dayAddress, ArrayList AllYeild, string type)
        {
            try
            {
                string ldclass;
                DateTime dt = DateTime.Now;
                string dt24 = dt.ToString("HH");
                string DT24 = dt.ToString("yyyyMMdd");
                //DateTime.Parse(DT24).ToString("yyyyMMdd");
                System.Diagnostics.Debug.WriteLine("时间:" + dt24 + "/" + int.Parse(dt24) + "/" + DT24);
                string today = dt.ToString("yyyyMMdd");
                if (int.Parse(dt24) < 8 || int.Parse(dt24) >= 20)//小于大于20点8点或者
                {
                    //晚班右边抛膜数据 日期修正
                    System.Diagnostics.Debug.WriteLine("班别：晚班" + dt24);
                    ldclass = "晚班";

                    if (int.Parse(dt24) < 8 && int.Parse(dt24) >= 0) //晚班时间减1判断
                    {
                        System.Diagnostics.Debug.WriteLine("日期DT24：" + DT24);
                        DT24 = (int.Parse(DT24) - 1).ToString();
                        System.Diagnostics.Debug.WriteLine("日期DT24-1：" + DT24);
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("dt24" + dt24 + "DT24:" + DT24);
                    }

                    //晚班右抛摸数据存在查询
                    string SqlStatus = (DbHelperSQL.GetNightMaxID("id", "dayyeild", DT24, type)).ToString();//查询
                    DateTime dt53 = DateTime.Now;
                    while ((DateTime.Now - dt53).TotalMilliseconds < 100)
                    {
                        continue;
                    };

                    Console.WriteLine("SqlStatus0:" + SqlStatus);
                    string strSql;
                    if (int.Parse(SqlStatus) <= 1)
                    {
                        //晚班晚班右边抛膜数据新增1条
                        Console.WriteLine("查询晚班数据条不存在" + SqlStatus);
                        strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[40] + "','" + AllYeild[42] + "','" + AllYeild[44] + "','" + AllYeild[46] + "','" + AllYeild[0] + "','" + AllYeild[2] + "','" + AllYeild[4] + "','" + AllYeild[6] + "','" + AllYeild[8] + "','" + AllYeild[10] + "','" + AllYeild[12] + "','" + AllYeild[14] + "','" + ldclass + "','" + type + "')";   //+ "9999" +"','" 
                        Console.WriteLine("strSql0:" + strSql);
                    }
                    else
                    {
                        //晚班右边抛膜数据更新1条
                        Console.WriteLine("查询数据条已经存在" + SqlStatus);
                        strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[40] + ",H0910=" + AllYeild[42] + ",H1011=" + AllYeild[44] + ",H1112=" + AllYeild[46] + ",H1213=" + AllYeild[0] + ",H1314=" + AllYeild[2] + ",H1415=" + AllYeild[4] + ",H1516=" + AllYeild[6] + ",H1617=" + AllYeild[8] + ",H1718=" + AllYeild[10] + ",H1819=" + AllYeild[12] + ",H1920=" + AllYeild[14] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus;
                        Console.WriteLine("strSql1:" + strSql);
                    }
                    //Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));
                    int sqlok = DbHelperSQL.ExecuteSql(strSql);//保存数据
                    DateTime dt54 = DateTime.Now;
                    while ((DateTime.Now - dt54).TotalMilliseconds < 100)
                    {
                        continue;
                    };

                    if (sqlok == 1 && run == true)
                    {
                        Console.WriteLine("DbHelperSQL晚班右抛摸数据操作1为成功,操作返回为:" + sqlok);
                    }
                    else
                    {
                        WriteLogs("log.txt", "RunLog", "晚班右抛摸数据库操作失败");
                    }
                }
                else
                {
                    ldclass = "白班";
                    System.Diagnostics.Debug.WriteLine("班别：" + ldclass+dt24);
                    string SqlStatus1 = (DbHelperSQL.GetDayMaxID("id", "dayyeild", today, type)).ToString();//查询
                    DateTime dt54 = DateTime.Now;
                    while ((DateTime.Now - dt54).TotalMilliseconds < 100)
                    {
                        continue;
                    };

                    Console.WriteLine("SqlStatus1:" + SqlStatus1);
                    string strSql;//白班数据库操作膜版
                    if (int.Parse(SqlStatus1) <= 1)
                    {
                        //白班右抛膜数据新增1条
                        Console.WriteLine("查询数据条不存在" + SqlStatus1);
                        strSql = "insert into " + DBtable[0] + "(" + dayAddress + ") values('" + DT24 + "','" + AllYeild[16] + "','" + AllYeild[18] + "','" + AllYeild[20] + "','" + AllYeild[22] + "','" + AllYeild[24] + "','" + AllYeild[26] + "','" + AllYeild[28] + "','" + AllYeild[30] + "','" + AllYeild[32] + "','" + AllYeild[34] + "','" + AllYeild[36] + "','" + AllYeild[38] + "','" + ldclass + "','" + type + "')";
                        Console.WriteLine("strSql1:" + strSql);

                    }
                    else
                    {
                        //白班右抛膜数据更新1条
                        Console.WriteLine("查询数据条已经存在" + SqlStatus1);
                        strSql = "update  " + DBtable[0] + " SET  date =" + DT24 + ",H0809=" + AllYeild[16] + ",H0910=" + AllYeild[18] + ",H1011=" + AllYeild[20] + ",H1112=" + AllYeild[22] + ",H1213=" + AllYeild[24] + ",H1314=" + AllYeild[26] + ",H1415=" + AllYeild[28] + ",H1516=" + AllYeild[30] + ",H1617=" + AllYeild[32] + ",H1718=" + AllYeild[34] + ",H1819=" + AllYeild[36] + ",H1920=" + AllYeild[38] + ",dclass='" + ldclass + "',type='" + type + "' where id=" + SqlStatus1;
                        Console.WriteLine("strSql1:" + strSql);
                    }
                    //Console.WriteLine("DbHelperSQL数据操作4,1为成功,操作返回为:" + DbHelperSQL.ExecuteSql(strSql));

                    int sqlok = DbHelperSQL.ExecuteSql(strSql);//查询是否存在
                    DateTime dt44 = DateTime.Now;
                    while ((DateTime.Now - dt44).TotalMilliseconds < 100)
                    {
                        continue;
                    };

                    if (sqlok == 1 && run == true)
                    {
                        Console.WriteLine("DbHelperSQL数据操作1为成功,操作返回为:" + sqlok);
                    }
                    else
                    {
                        WriteLogs("log.txt", "RunLog", "白班右抛摸数据库操作");
                    }
                }
                AllYeild.Clear(); //清除数组列表
            }
            catch (Exception e)
            {
                WriteLogs("log.txt", "Exception:", e.Message.ToString());
            }
            return;
        }


        //写出日志
        public static void WriteLogs(string fileName, string type, string content)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            if (!string.IsNullOrEmpty(path))
            {
                path = AppDomain.CurrentDomain.BaseDirectory + fileName;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path = path + "\\" + DateTime.Now.ToString("yyyyMMdd");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path = path + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                if (!File.Exists(path))
                {
                    FileStream fs = File.Create(path);
                    fs.Close();
                }
                if (File.Exists(path))
                {
                    StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default);
                    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--" + type + "-->" + content);
                    sw.WriteLine("----------------------------------------");
                    sw.Flush();
                    sw.Close();
                    //Console.WriteLine("yyyy-MM-dd HH:mm:ss.fff" + type + "-->" + content + path);
                }
                //Console.WriteLine("yyyy-MM-dd HH:mm:ss.fff" + type + "-->" + content + path);
            }
            return;
        }


        //函数循环定时器函数
        public void theout(object source, System.Timers.ElapsedEventArgs e)

        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//设置该属性 为false
            Console.WriteLine("定时器运行");

            ReadBugTime3(null, null);
            DateTime dt1 = DateTime.Now;
            while ((DateTime.Now - dt1).TotalMilliseconds < 300)
            {
                continue;
            };

            ReadWaitTime2(null, null);
            DateTime dt11 = DateTime.Now;
            while ((DateTime.Now - dt11).TotalMilliseconds < 300)
            {
                continue;
            };

            ReadYoupao5(null, null);
            DateTime dt111 = DateTime.Now;
            while ((DateTime.Now - dt111).TotalMilliseconds < 300)
            {
                continue;
            };

            ReadZuopao4(null, null);
            DateTime dt1111 = DateTime.Now;
            while ((DateTime.Now - dt1111).TotalMilliseconds < 300)
            {
                continue;
            };
            ReadBtn_Click(null, null);

            //清理内存
            GC.Collect();
            GC.WaitForPendingFinalizers();
            System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process process in processes)
            {
                //以下系统进程没有权限，所以跳过，防止出错影响效率。  
                if ((process.ProcessName == "System") && (process.ProcessName == "Idle"))
                    continue;

            }
        }

       
    }
}







